2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-12-22 02:34:37 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module ast
|
|
|
|
|
|
|
|
import (
|
2019-12-27 05:43:17 +01:00
|
|
|
v.token
|
2020-01-22 21:34:38 +01:00
|
|
|
v.table
|
2019-12-22 02:34:37 +01:00
|
|
|
)
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
2020-02-03 07:44:52 +01:00
|
|
|
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
2020-02-02 14:31:54 +01:00
|
|
|
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr
|
2019-12-28 14:11:05 +01:00
|
|
|
|
2020-02-03 07:44:52 +01:00
|
|
|
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
2020-02-04 09:54:15 +01:00
|
|
|
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
|
|
|
HashStmt
|
2020-01-06 16:13:12 +01:00
|
|
|
// | IncDecStmt k
|
2019-12-28 14:11:05 +01:00
|
|
|
// Stand-alone expression in a statement list.
|
|
|
|
pub struct ExprStmt {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
|
2019-12-26 11:21:41 +01:00
|
|
|
pub struct IntegerLiteral {
|
2019-12-26 03:40:18 +01:00
|
|
|
pub:
|
|
|
|
val int
|
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
|
2019-12-27 10:03:29 +01:00
|
|
|
pub struct FloatLiteral {
|
|
|
|
pub:
|
2019-12-28 09:43:22 +01:00
|
|
|
// val f64
|
2019-12-27 10:03:29 +01:00
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:54:43 +01:00
|
|
|
pub struct StringLiteral {
|
|
|
|
pub:
|
|
|
|
val string
|
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
pub struct CharLiteral {
|
|
|
|
pub:
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2019-12-29 08:51:55 +01:00
|
|
|
pub struct BoolLiteral {
|
|
|
|
pub:
|
|
|
|
val bool
|
|
|
|
}
|
|
|
|
|
2020-01-02 20:09:15 +01:00
|
|
|
// `foo.bar`
|
|
|
|
pub struct SelectorExpr {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
2020-01-02 20:09:15 +01:00
|
|
|
expr Expr
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
|
2019-12-28 11:02:06 +01:00
|
|
|
// module declaration
|
2019-12-28 09:15:32 +01:00
|
|
|
pub struct Module {
|
|
|
|
pub:
|
2019-12-28 09:43:22 +01:00
|
|
|
name string
|
|
|
|
path string
|
|
|
|
expr Expr
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 06:16:59 +01:00
|
|
|
pub struct Field {
|
|
|
|
pub:
|
|
|
|
name string
|
2020-01-18 23:26:14 +01:00
|
|
|
// type_idx int
|
2020-02-03 07:02:54 +01:00
|
|
|
typ table.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConstDecl {
|
|
|
|
pub:
|
|
|
|
fields []Field
|
|
|
|
exprs []Expr
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructDecl {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
2019-12-30 06:16:59 +01:00
|
|
|
name string
|
|
|
|
fields []Field
|
2019-12-31 19:42:16 +01:00
|
|
|
is_pub bool
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructInit {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2019-12-30 06:16:59 +01:00
|
|
|
fields []string
|
|
|
|
exprs []Expr
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:15:32 +01:00
|
|
|
// import statement
|
|
|
|
pub struct Import {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
|
|
|
mod string
|
|
|
|
alias string
|
2019-12-31 19:42:16 +01:00
|
|
|
// expr Expr
|
2019-12-28 09:15:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 07:24:17 +01:00
|
|
|
pub struct Arg {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2019-12-29 07:24:17 +01:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2019-12-27 13:57:49 +01:00
|
|
|
pub struct FnDecl {
|
|
|
|
pub:
|
2020-01-06 16:13:12 +01:00
|
|
|
name string
|
|
|
|
stmts []Stmt
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2020-01-06 16:13:12 +01:00
|
|
|
args []Arg
|
|
|
|
is_pub bool
|
2020-01-02 20:09:15 +01:00
|
|
|
receiver Field
|
2019-12-29 07:24:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 08:29:50 +01:00
|
|
|
pub struct BranchStmt {
|
|
|
|
pub:
|
|
|
|
tok token.Token
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:24:17 +01:00
|
|
|
pub struct CallExpr {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
// tok token.Token
|
|
|
|
pos token.Position
|
2020-01-18 23:26:14 +01:00
|
|
|
mut:
|
2020-01-22 21:34:38 +01:00
|
|
|
// func Expr
|
|
|
|
name string
|
|
|
|
args []Expr
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MethodCallExpr {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
// tok token.Token
|
|
|
|
pos token.Position
|
|
|
|
expr Expr
|
|
|
|
name string
|
|
|
|
args []Expr
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Return {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
pos token.Position
|
|
|
|
expected_ti table.Type // TODO: remove once checker updated
|
|
|
|
exprs []Expr
|
2019-12-27 13:57:49 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
/*
|
|
|
|
pub enum Expr {
|
2020-01-22 21:34:38 +01:00
|
|
|
Binary(InfixExpr)
|
2019-12-22 02:34:37 +01:00
|
|
|
If(IfExpr)
|
|
|
|
Integer(IntegerExpr)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-12-24 18:54:43 +01:00
|
|
|
/*
|
2019-12-22 02:34:37 +01:00
|
|
|
pub struct Stmt {
|
|
|
|
pos int
|
|
|
|
//end int
|
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-28 09:43:22 +01:00
|
|
|
|
2019-12-24 18:54:43 +01:00
|
|
|
pub struct VarDecl {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
name string
|
|
|
|
expr Expr
|
2020-01-18 23:26:14 +01:00
|
|
|
is_mut bool
|
2020-01-22 21:34:38 +01:00
|
|
|
mut:
|
|
|
|
typ table.Type
|
|
|
|
pos token.Position
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
pub struct GlobalDecl {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
expr Expr
|
|
|
|
mut:
|
|
|
|
typ table.Type
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:10:46 +01:00
|
|
|
pub struct File {
|
2019-12-24 18:54:43 +01:00
|
|
|
pub:
|
2020-02-03 07:31:54 +01:00
|
|
|
path string
|
2020-01-18 23:26:14 +01:00
|
|
|
mod Module
|
|
|
|
imports []Import
|
|
|
|
stmts []Stmt
|
2020-02-04 12:03:12 +01:00
|
|
|
unresolved []Expr
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IdentVar {
|
|
|
|
pub:
|
2020-01-22 21:34:38 +01:00
|
|
|
typ table.Type
|
2020-02-04 12:03:12 +01:00
|
|
|
//name string
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type IdentInfo = IdentVar
|
|
|
|
|
|
|
|
pub enum IdentKind {
|
|
|
|
blank_ident
|
|
|
|
variable
|
2020-02-03 07:02:54 +01:00
|
|
|
constant
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
// A single identifier
|
2019-12-28 14:11:05 +01:00
|
|
|
pub struct Ident {
|
|
|
|
pub:
|
|
|
|
name string
|
2019-12-31 10:53:30 +01:00
|
|
|
tok_kind token.Kind
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
2019-12-28 09:43:22 +01:00
|
|
|
value string
|
2020-01-18 23:26:14 +01:00
|
|
|
mut:
|
|
|
|
kind IdentKind
|
|
|
|
info IdentInfo
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
pub struct InfixExpr {
|
2019-12-22 02:34:37 +01:00
|
|
|
pub:
|
2019-12-28 19:16:04 +01:00
|
|
|
// op BinaryOp
|
2020-01-22 21:34:38 +01:00
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
|
|
|
left Expr
|
|
|
|
left_type table.Type
|
|
|
|
right Expr
|
|
|
|
right_type table.Type
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
/*
|
|
|
|
// renamed to PrefixExpr
|
2019-12-25 13:39:58 +01:00
|
|
|
pub struct UnaryExpr {
|
|
|
|
pub:
|
2019-12-31 10:53:30 +01:00
|
|
|
// tok_kind token.Kind
|
2019-12-28 09:43:22 +01:00
|
|
|
// op BinaryOp
|
2019-12-31 10:53:30 +01:00
|
|
|
op token.Kind
|
2019-12-28 09:43:22 +01:00
|
|
|
left Expr
|
2019-12-25 13:39:58 +01:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-25 13:39:58 +01:00
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
pub struct PostfixExpr {
|
|
|
|
pub:
|
|
|
|
op token.Kind
|
|
|
|
expr Expr
|
2020-02-04 07:37:38 +01:00
|
|
|
pos token.Position
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PrefixExpr {
|
|
|
|
pub:
|
|
|
|
op token.Kind
|
|
|
|
right Expr
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:14:10 +01:00
|
|
|
pub struct IndexExpr {
|
|
|
|
pub:
|
|
|
|
// op token.Kind
|
2020-02-03 07:44:52 +01:00
|
|
|
pos token.Position
|
2020-01-07 12:14:10 +01:00
|
|
|
left Expr
|
2020-01-22 21:34:38 +01:00
|
|
|
index Expr // [0], [start..end] etc
|
2020-02-03 11:29:50 +01:00
|
|
|
// typ table.Type
|
2020-01-07 12:14:10 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 19:16:04 +01:00
|
|
|
pub struct IfExpr {
|
2019-12-29 08:51:55 +01:00
|
|
|
pub:
|
2020-01-01 22:34:46 +01:00
|
|
|
tok_kind token.Kind
|
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
2019-12-31 19:42:16 +01:00
|
|
|
else_stmts []Stmt
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2020-01-02 20:09:15 +01:00
|
|
|
left Expr // `a` in `a := if ...`
|
2020-02-03 07:02:54 +01:00
|
|
|
pos token.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CompIf {
|
|
|
|
pub:
|
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
|
|
|
else_stmts []Stmt
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 06:16:59 +01:00
|
|
|
pub struct ForStmt {
|
|
|
|
pub:
|
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
2020-02-03 07:02:54 +01:00
|
|
|
pos token.Position
|
2020-01-07 00:14:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ForInStmt {
|
|
|
|
pub:
|
|
|
|
var string
|
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ForCStmt {
|
|
|
|
pub:
|
|
|
|
init Stmt // i := 0;
|
|
|
|
cond Expr // i < 10;
|
|
|
|
inc Stmt // i++;
|
|
|
|
stmts []Stmt
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 19:16:04 +01:00
|
|
|
pub struct ReturnStmt {
|
2019-12-31 10:53:30 +01:00
|
|
|
tok_kind token.Kind // or pos
|
2020-01-22 21:34:38 +01:00
|
|
|
pos token.Position
|
2019-12-28 09:43:22 +01:00
|
|
|
results []Expr
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
// #include etc
|
|
|
|
pub struct HashStmt {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-01-06 23:15:37 +01:00
|
|
|
/*
|
2019-12-28 19:16:04 +01:00
|
|
|
pub struct AssignStmt {
|
|
|
|
pub:
|
|
|
|
left Expr
|
|
|
|
right Expr
|
2019-12-31 10:53:30 +01:00
|
|
|
op token.Kind
|
2019-12-28 19:16:04 +01:00
|
|
|
}
|
2020-01-06 23:15:37 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
// e.g. `[unsafe_fn]`
|
|
|
|
pub struct Attr {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
}
|
2019-12-28 19:16:04 +01:00
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
pub struct AssignExpr {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
2020-01-06 16:13:12 +01:00
|
|
|
left Expr
|
|
|
|
val Expr
|
|
|
|
}
|
|
|
|
|
2019-12-30 09:38:12 +01:00
|
|
|
pub struct ArrayInit {
|
|
|
|
pub:
|
2020-01-19 13:52:34 +01:00
|
|
|
pos token.Position
|
2019-12-30 09:38:12 +01:00
|
|
|
exprs []Expr
|
2020-01-22 21:34:38 +01:00
|
|
|
ti table.Type
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:31:54 +01:00
|
|
|
// s[10..20]
|
|
|
|
pub struct RangeExpr {
|
|
|
|
pub:
|
|
|
|
low Expr
|
|
|
|
high Expr
|
|
|
|
}
|
|
|
|
|
2019-12-25 13:39:58 +01:00
|
|
|
// string representaiton of expr
|
|
|
|
pub fn (x Expr) str() string {
|
|
|
|
match x {
|
2020-01-22 21:34:38 +01:00
|
|
|
InfixExpr {
|
2019-12-26 11:27:35 +01:00
|
|
|
return '(${it.left.str()} $it.op.str() ${it.right.str()})'
|
2019-12-25 13:39:58 +01:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
/*
|
|
|
|
PrefixExpr {
|
2019-12-26 11:27:35 +01:00
|
|
|
return it.left.str() + it.op.str()
|
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-26 11:27:35 +01:00
|
|
|
IntegerLiteral {
|
|
|
|
return it.val.str()
|
|
|
|
}
|
|
|
|
IntegerLiteral {
|
|
|
|
return '"$it.val"'
|
2019-12-25 13:39:58 +01:00
|
|
|
}
|
2019-12-28 09:43:22 +01:00
|
|
|
else {
|
|
|
|
return ''
|
|
|
|
}
|
2019-12-25 13:39:58 +01:00
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 14:11:05 +01:00
|
|
|
pub fn (node Stmt) str() string {
|
|
|
|
match node {
|
|
|
|
VarDecl {
|
|
|
|
return it.name + ' = ' + it.expr.str()
|
|
|
|
}
|
|
|
|
ExprStmt {
|
|
|
|
return it.expr.str()
|
|
|
|
}
|
|
|
|
FnDecl {
|
|
|
|
return 'fn ${it.name}() { $it.stmts.len stmts }'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return '[unhandled stmt str]'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 03:40:18 +01:00
|
|
|
/*
|
|
|
|
enum BinaryOp {
|
|
|
|
sum
|
|
|
|
difference
|
|
|
|
product
|
|
|
|
quotient
|
|
|
|
remainder
|
|
|
|
bitwise_and
|
|
|
|
bitwise_or
|
|
|
|
bitwise_xor
|
|
|
|
left_shift
|
|
|
|
right_shift
|
|
|
|
|
|
|
|
equality
|
|
|
|
inequality
|
|
|
|
less_than
|
|
|
|
less_than_or_equal
|
|
|
|
more_than
|
|
|
|
more_than_or_equal
|
|
|
|
|
|
|
|
in_check
|
|
|
|
|
|
|
|
//These are suffixed with `bool` to prevent conflict with the keyword `or`
|
|
|
|
and_bool
|
|
|
|
or_bool
|
|
|
|
}
|
|
|
|
*/
|