about summary refs log tree commit diff
path: root/pkg/lang
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-04-20 02:48:45 +0200
committerMel <einebeere@gmail.com>2022-04-20 02:48:45 +0200
commit20bc0c2243888900d2f04328e57ad2d7ca0a2403 (patch)
tree6f0fc535fd8a74eb8f91c4a35fa3c32f012f0a59 /pkg/lang
parentdb3dd28cdf9009880c1ab5f12c47b673dd00bc43 (diff)
downloadjinx-20bc0c2243888900d2f04328e57ad2d7ca0a2403.tar.zst
jinx-20bc0c2243888900d2f04328e57ad2d7ca0a2403.zip
Basic AST for lang
Diffstat (limited to 'pkg/lang')
-rw-r--r--pkg/lang/ast/ast.go5
-rw-r--r--pkg/lang/ast/expr.go87
-rw-r--r--pkg/lang/ast/nodes.go18
-rw-r--r--pkg/lang/ast/op.go26
-rw-r--r--pkg/lang/ast/stmt.go74
5 files changed, 210 insertions, 0 deletions
diff --git a/pkg/lang/ast/ast.go b/pkg/lang/ast/ast.go
new file mode 100644
index 0000000..43d0c28
--- /dev/null
+++ b/pkg/lang/ast/ast.go
@@ -0,0 +1,5 @@
+package ast
+
+type Program struct {
+	Stmts []Stmt[any]
+}
\ No newline at end of file
diff --git a/pkg/lang/ast/expr.go b/pkg/lang/ast/expr.go
new file mode 100644
index 0000000..a4d6f56
--- /dev/null
+++ b/pkg/lang/ast/expr.go
@@ -0,0 +1,87 @@
+package ast
+
+import "jinx/pkg/lang/scanner/token"
+
+type ExprKind int
+
+const (
+	ExprKindBinary ExprKind = iota
+	ExprKindUnary
+	ExprKindCall
+	ExprKindSubscription
+
+	ExprKindGroup
+	ExprKindFnLit
+	ExprKindArrayLit
+	ExprKindIdent
+	ExprKindIntLit
+	ExprKindFloatLit
+	ExprKindStringLit
+	ExprKindBoolLit
+	ExprKindNullLit
+	ExprKindThis
+)
+
+type Expr[T any] struct {
+	At    token.Loc
+	Kind  ExprKind
+	Value T
+}
+
+type ExprBinary struct {
+	Left  Expr[any]
+	Op    BinOp
+	Right Expr[any]
+}
+
+type ExprUnary struct {
+	Op    UnOp
+	Value Expr[any]
+}
+
+type ExprCall struct {
+	Callee Expr[any]
+	Args   []Expr[any]
+}
+
+type ExprSubscription struct {
+	Obj Expr[any]
+	Key Expr[any]
+}
+
+type ExprGroup struct {
+	Value Expr[any]
+}
+
+type ExprFnLit struct {
+	Args []IdentNode
+	Body BlockNode
+}
+
+type ExprArrayLit struct {
+	Values []Expr[any]
+}
+
+type ExprIdent struct {
+	Value IdentNode
+}
+
+type ExprIntLit struct {
+	Value uint64
+}
+
+type ExprFloatLit struct {
+	Value float64
+}
+
+type ExprStringLit struct {
+	Value string
+}
+
+type ExprBoolLit struct {
+	Value bool
+}
+
+type ExprNullLit struct{}
+
+type ExprThis struct{}
diff --git a/pkg/lang/ast/nodes.go b/pkg/lang/ast/nodes.go
new file mode 100644
index 0000000..699d025
--- /dev/null
+++ b/pkg/lang/ast/nodes.go
@@ -0,0 +1,18 @@
+package ast
+
+import "jinx/pkg/lang/scanner/token"
+
+type IdentNode struct {
+	At    token.Loc
+	Value string
+}
+
+type BlockNode struct {
+	At    token.Loc
+	Stmts []Stmt[any]
+}
+
+type CondNode struct {
+	Cond Expr[any]
+	Then BlockNode
+}
diff --git a/pkg/lang/ast/op.go b/pkg/lang/ast/op.go
new file mode 100644
index 0000000..a708c78
--- /dev/null
+++ b/pkg/lang/ast/op.go
@@ -0,0 +1,26 @@
+package ast
+
+type BinOp int
+
+const (
+	BinOpAdd BinOp = iota
+	BinOpSub
+	BinOpMul
+	BinOpDiv
+	BinOpMod
+
+	BinOpAssign
+	BinOpEq
+	BinOpNeq
+	BinOpLt
+	BinOpLte
+	BinOpGt
+	BinOpGte
+)
+
+type UnOp int
+
+const (
+	UnOpNot UnOp = iota
+	UnOpNeg
+)
diff --git a/pkg/lang/ast/stmt.go b/pkg/lang/ast/stmt.go
new file mode 100644
index 0000000..e924f8c
--- /dev/null
+++ b/pkg/lang/ast/stmt.go
@@ -0,0 +1,74 @@
+package ast
+
+import "jinx/pkg/lang/scanner/token"
+
+type StmtKind int
+
+const (
+	StmtKindUse StmtKind = iota
+	StmtKindFnDecl
+	StmtKindObjectDecl
+	StmtKindVarDecl
+	StmtKindIf
+	StmtKindTry
+	StmtKindReturn
+	StmtKindContinue
+	StmtKindBreak
+	StmtKindThrow
+	StmtKindExpr
+)
+
+type Stmt[T any] struct {
+	At    token.Loc
+	Kind  StmtKind
+	Value T
+}
+
+type StmtUse struct {
+	Globals []IdentNode
+	Modules []IdentNode
+	Author  IdentNode
+}
+
+type StmtFnDecl struct {
+	Name IdentNode
+	Args []IdentNode
+	Body BlockNode
+}
+
+type StmtObjectDecl struct{}
+
+type StmtVarDecl struct {
+	Name  IdentNode
+	Value Expr[any]
+}
+
+type StmtIf struct {
+	Cond  Expr[any]
+	Then  BlockNode
+	Elifs []CondNode
+	Else  BlockNode
+}
+
+type StmtTry struct {
+	Try         BlockNode
+	CatchedName IdentNode
+	Catch       BlockNode
+	Finally     BlockNode
+}
+
+type StmtReturn struct {
+	Value Expr[any]
+}
+
+type StmtContinue struct{}
+
+type StmtBreak struct{}
+
+type StmtThrow struct {
+	Value Expr[any]
+}
+
+type StmtExpr struct {
+	Value Expr[any]
+}