2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-12-22 04:34:37 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module ast
|
|
|
|
|
2020-04-19 01:07:57 +03:00
|
|
|
import v.token
|
|
|
|
import v.table
|
2020-05-10 12:26:57 +03:00
|
|
|
import v.errors
|
2019-12-22 04:34:37 +03:00
|
|
|
|
2020-04-25 21:58:00 +03:00
|
|
|
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
|
2020-03-07 19:37:55 +03:00
|
|
|
|
2020-05-18 19:08:28 +03:00
|
|
|
pub type Expr = AnonFn | ArrayInit | AsCast | AssignExpr | Assoc | BoolLiteral | CallExpr |
|
2020-05-27 04:20:22 +03:00
|
|
|
CastExpr | CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr |
|
|
|
|
IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | MapInit | MatchExpr | None | OrExpr |
|
|
|
|
ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | StringInterLiteral |
|
2020-06-09 17:36:18 +03:00
|
|
|
StringLiteral | StructInit | Type | TypeOf | Likely
|
2020-05-18 19:08:28 +03:00
|
|
|
|
2020-05-27 04:20:22 +03:00
|
|
|
pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf | ConstDecl |
|
|
|
|
DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt |
|
|
|
|
GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module | Return | StructDecl | TypeDecl |
|
|
|
|
UnsafeStmt
|
2020-04-04 06:14:40 +03:00
|
|
|
|
|
|
|
pub type ScopeObject = ConstField | GlobalDecl | Var
|
|
|
|
|
2020-03-03 17:06:21 +03:00
|
|
|
// pub type Type = StructType | ArrayType
|
|
|
|
// pub struct StructType {
|
2020-03-04 17:48:43 +03:00
|
|
|
// fields []Field
|
2020-03-03 17:06:21 +03:00
|
|
|
// }
|
|
|
|
// pub struct ArrayType {}
|
|
|
|
pub struct Type {
|
2020-03-02 12:53:38 +03:00
|
|
|
pub:
|
2020-03-04 17:48:43 +03:00
|
|
|
typ table.Type
|
2020-04-18 01:19:33 +03:00
|
|
|
pos token.Position
|
2020-03-02 12:53:38 +03:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:44:17 +03:00
|
|
|
pub struct Block {
|
|
|
|
pub:
|
|
|
|
stmts []Stmt
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:13:12 +03:00
|
|
|
// | IncDecStmt k
|
2019-12-28 16:11:05 +03:00
|
|
|
// Stand-alone expression in a statement list.
|
|
|
|
pub struct ExprStmt {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
2020-04-07 17:36:00 +03:00
|
|
|
pos token.Position
|
2020-05-21 23:35:43 +03:00
|
|
|
pub mut:
|
|
|
|
typ table.Type
|
2019-12-28 16:11:05 +03:00
|
|
|
}
|
2019-12-22 04:34:37 +03:00
|
|
|
|
2019-12-26 13:21:41 +03:00
|
|
|
pub struct IntegerLiteral {
|
2019-12-26 05:40:18 +03:00
|
|
|
pub:
|
2020-03-17 04:49:15 +03:00
|
|
|
val string
|
2020-04-10 01:09:34 +03:00
|
|
|
pos token.Position
|
2019-12-26 05:40:18 +03:00
|
|
|
}
|
2019-12-24 20:54:43 +03:00
|
|
|
|
2019-12-27 12:03:29 +03:00
|
|
|
pub struct FloatLiteral {
|
|
|
|
pub:
|
|
|
|
val string
|
2020-04-20 15:49:26 +03:00
|
|
|
pos token.Position
|
2019-12-27 12:03:29 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:54:43 +03:00
|
|
|
pub struct StringLiteral {
|
|
|
|
pub:
|
2020-05-19 18:12:47 +03:00
|
|
|
val string
|
|
|
|
is_raw bool
|
|
|
|
language table.Language
|
|
|
|
pos token.Position
|
2019-12-24 20:54:43 +03:00
|
|
|
}
|
2019-12-22 04:34:37 +03:00
|
|
|
|
2020-03-21 09:01:06 +03:00
|
|
|
// 'name: $name'
|
|
|
|
pub struct StringInterLiteral {
|
|
|
|
pub:
|
|
|
|
vals []string
|
|
|
|
exprs []Expr
|
2020-06-16 11:41:51 +03:00
|
|
|
fwidths []int
|
|
|
|
precisions []int
|
|
|
|
pluss []bool
|
|
|
|
fills []bool
|
|
|
|
fmt_poss []token.Position
|
2020-04-10 11:59:07 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-21 09:01:06 +03:00
|
|
|
expr_types []table.Type
|
2020-06-16 11:41:51 +03:00
|
|
|
fmts []byte
|
|
|
|
need_fmts []bool // an explicit non-default fmt required, e.g. `x`
|
2020-03-21 09:01:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 11:54:15 +03:00
|
|
|
pub struct CharLiteral {
|
|
|
|
pub:
|
|
|
|
val string
|
2020-04-20 15:49:26 +03:00
|
|
|
pos token.Position
|
2020-02-04 11:54:15 +03:00
|
|
|
}
|
|
|
|
|
2019-12-29 10:51:55 +03:00
|
|
|
pub struct BoolLiteral {
|
|
|
|
pub:
|
|
|
|
val bool
|
2020-04-20 15:49:26 +03:00
|
|
|
pos token.Position
|
2019-12-29 10:51:55 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 22:09:15 +03:00
|
|
|
// `foo.bar`
|
|
|
|
pub struct SelectorExpr {
|
|
|
|
pub:
|
2020-05-18 19:06:09 +03:00
|
|
|
pos token.Position
|
|
|
|
expr Expr
|
|
|
|
field_name string
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-05-18 19:06:09 +03:00
|
|
|
expr_type table.Type
|
2020-01-02 22:09:15 +03:00
|
|
|
}
|
|
|
|
|
2019-12-28 13:02:06 +03:00
|
|
|
// module declaration
|
2019-12-28 11:15:32 +03:00
|
|
|
pub struct Module {
|
|
|
|
pub:
|
2020-04-19 01:07:57 +03:00
|
|
|
name string
|
|
|
|
path string
|
|
|
|
expr Expr
|
2020-05-16 17:12:23 +03:00
|
|
|
pos token.Position
|
2020-04-10 23:32:52 +03:00
|
|
|
is_skipped bool // module main can be skipped in single file programs
|
2019-12-28 11:15:32 +03:00
|
|
|
}
|
|
|
|
|
2020-04-05 03:08:10 +03:00
|
|
|
pub struct StructField {
|
|
|
|
pub:
|
2020-04-12 13:35:54 +03:00
|
|
|
name string
|
|
|
|
pos token.Position
|
|
|
|
comment Comment
|
|
|
|
default_expr Expr
|
|
|
|
has_default_expr bool
|
2020-05-09 16:16:48 +03:00
|
|
|
attrs []string
|
2020-05-18 19:06:09 +03:00
|
|
|
is_public bool
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-12 13:35:54 +03:00
|
|
|
typ table.Type
|
2020-04-05 03:08:10 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 08:16:59 +03:00
|
|
|
pub struct Field {
|
|
|
|
pub:
|
|
|
|
name string
|
2020-04-09 16:33:46 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-02-10 10:32:08 +03:00
|
|
|
typ table.Type
|
2020-02-03 09:02:54 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:14:40 +03:00
|
|
|
pub struct ConstField {
|
2020-02-03 09:02:54 +03:00
|
|
|
pub:
|
2020-05-18 19:06:09 +03:00
|
|
|
name string
|
|
|
|
expr Expr
|
|
|
|
is_pub bool
|
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-05-18 19:06:09 +03:00
|
|
|
typ table.Type
|
2020-05-12 01:09:59 +03:00
|
|
|
comment Comment
|
2020-04-04 06:14:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConstDecl {
|
|
|
|
pub:
|
2020-02-27 00:43:37 +03:00
|
|
|
is_pub bool
|
2020-04-04 06:14:40 +03:00
|
|
|
pos token.Position
|
2020-04-27 23:53:26 +03:00
|
|
|
pub mut:
|
|
|
|
fields []ConstField
|
2019-12-30 08:16:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructDecl {
|
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
pos token.Position
|
|
|
|
name string
|
|
|
|
fields []StructField
|
|
|
|
is_pub bool
|
|
|
|
mut_pos int // mut:
|
|
|
|
pub_pos int // pub:
|
|
|
|
pub_mut_pos int // pub mut:
|
2020-05-19 18:12:47 +03:00
|
|
|
language table.Language
|
2020-04-09 16:33:46 +03:00
|
|
|
is_union bool
|
2020-06-15 15:59:09 +03:00
|
|
|
attrs []string
|
2019-12-30 08:16:59 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:59:38 +03:00
|
|
|
pub struct InterfaceDecl {
|
2020-04-05 13:31:39 +03:00
|
|
|
pub:
|
2020-03-31 21:26:15 +03:00
|
|
|
name string
|
|
|
|
field_names []string
|
2020-04-22 21:20:49 +03:00
|
|
|
methods []FnDecl
|
2020-05-18 19:06:09 +03:00
|
|
|
pos token.Position
|
2020-03-31 20:59:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-17 03:38:39 +03:00
|
|
|
pub struct StructInitField {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
2020-04-19 01:07:57 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-27 23:53:26 +03:00
|
|
|
name string
|
2020-04-17 03:38:39 +03:00
|
|
|
typ table.Type
|
|
|
|
expected_type table.Type
|
|
|
|
}
|
|
|
|
|
2019-12-30 08:16:59 +03:00
|
|
|
pub struct StructInit {
|
|
|
|
pub:
|
2020-04-17 03:38:39 +03:00
|
|
|
pos token.Position
|
|
|
|
is_short bool
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-17 03:38:39 +03:00
|
|
|
typ table.Type
|
2020-04-27 23:53:26 +03:00
|
|
|
fields []StructInitField
|
2019-12-30 08:16:59 +03:00
|
|
|
}
|
|
|
|
|
2019-12-28 11:15:32 +03:00
|
|
|
// import statement
|
|
|
|
pub struct Import {
|
|
|
|
pub:
|
2020-01-19 01:26:14 +03:00
|
|
|
pos token.Position
|
|
|
|
mod string
|
|
|
|
alias string
|
2019-12-28 11:15:32 +03:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:59:19 +03:00
|
|
|
pub struct AnonFn {
|
|
|
|
pub:
|
|
|
|
decl FnDecl
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-19 01:07:57 +03:00
|
|
|
typ table.Type
|
2020-04-17 22:59:19 +03:00
|
|
|
}
|
|
|
|
|
2019-12-27 15:57:49 +03:00
|
|
|
pub struct FnDecl {
|
|
|
|
pub:
|
2020-03-05 18:13:14 +03:00
|
|
|
name string
|
2020-06-11 21:02:27 +03:00
|
|
|
mod string
|
2020-03-05 18:13:14 +03:00
|
|
|
stmts []Stmt
|
2020-03-15 01:21:36 +03:00
|
|
|
args []table.Arg
|
2020-03-05 18:13:14 +03:00
|
|
|
is_deprecated bool
|
|
|
|
is_pub bool
|
|
|
|
is_variadic bool
|
2020-04-17 22:59:19 +03:00
|
|
|
is_anon bool
|
2020-03-05 18:13:14 +03:00
|
|
|
receiver Field
|
2020-05-07 11:14:51 +03:00
|
|
|
receiver_pos token.Position
|
2020-03-05 18:13:14 +03:00
|
|
|
is_method bool
|
|
|
|
rec_mut bool // is receiver mutable
|
2020-05-19 18:12:47 +03:00
|
|
|
language table.Language
|
2020-03-06 18:31:40 +03:00
|
|
|
no_body bool // just a definition `fn C.malloc()`
|
2020-04-10 19:11:43 +03:00
|
|
|
is_builtin bool // this function is defined in builtin/strconv
|
2020-04-25 16:57:11 +03:00
|
|
|
ctdefine string // has [if myflag] tag
|
2020-03-21 21:52:19 +03:00
|
|
|
pos token.Position
|
2020-05-04 13:31:34 +03:00
|
|
|
body_pos token.Position
|
2020-05-03 22:13:40 +03:00
|
|
|
file string
|
2020-05-21 04:58:50 +03:00
|
|
|
is_generic bool
|
2020-04-27 23:53:26 +03:00
|
|
|
pub mut:
|
|
|
|
return_type table.Type
|
2019-12-29 09:24:17 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:29:50 +03:00
|
|
|
pub struct BranchStmt {
|
|
|
|
pub:
|
|
|
|
tok token.Token
|
|
|
|
}
|
|
|
|
|
2019-12-29 09:24:17 +03:00
|
|
|
pub struct CallExpr {
|
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
pos token.Position
|
|
|
|
left Expr // `user` in `user.register()`
|
|
|
|
mod string
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-09 16:33:46 +03:00
|
|
|
name string
|
2020-04-27 23:53:26 +03:00
|
|
|
is_method bool
|
2020-04-09 16:33:46 +03:00
|
|
|
args []CallArg
|
2020-04-03 16:18:17 +03:00
|
|
|
expected_arg_types []table.Type
|
2020-05-19 18:12:47 +03:00
|
|
|
language table.Language
|
2020-04-09 16:33:46 +03:00
|
|
|
or_block OrExpr
|
|
|
|
left_type table.Type // type of `user`
|
|
|
|
receiver_type table.Type // User
|
|
|
|
return_type table.Type
|
2020-04-25 16:57:11 +03:00
|
|
|
should_be_skipped bool
|
2020-05-21 04:58:50 +03:00
|
|
|
generic_type table.Type // TODO array, to support multiple types
|
2020-03-14 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CallArg {
|
|
|
|
pub:
|
2020-03-26 13:09:59 +03:00
|
|
|
is_mut bool
|
|
|
|
expr Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-26 13:09:59 +03:00
|
|
|
typ table.Type
|
2019-12-27 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Return {
|
|
|
|
pub:
|
2020-03-06 15:43:22 +03:00
|
|
|
pos token.Position
|
|
|
|
exprs []Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-16 09:42:45 +03:00
|
|
|
types []table.Type
|
2019-12-27 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 04:34:37 +03:00
|
|
|
/*
|
|
|
|
pub enum Expr {
|
2020-01-22 23:34:38 +03:00
|
|
|
Binary(InfixExpr)
|
2019-12-22 04:34:37 +03:00
|
|
|
If(IfExpr)
|
|
|
|
Integer(IntegerExpr)
|
|
|
|
}
|
|
|
|
*/
|
2019-12-24 20:54:43 +03:00
|
|
|
/*
|
2019-12-22 04:34:37 +03:00
|
|
|
pub struct Stmt {
|
|
|
|
pos int
|
|
|
|
//end int
|
|
|
|
}
|
2019-12-24 20:54:43 +03:00
|
|
|
*/
|
2020-03-10 14:01:37 +03:00
|
|
|
pub struct Var {
|
2019-12-24 20:54:43 +03:00
|
|
|
pub:
|
2020-06-02 10:00:51 +03:00
|
|
|
name string
|
|
|
|
expr Expr
|
|
|
|
is_mut bool
|
|
|
|
is_arg bool // fn args should not be autofreed
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-06-02 10:00:51 +03:00
|
|
|
typ table.Type
|
|
|
|
pos token.Position
|
|
|
|
is_used bool
|
|
|
|
is_changed bool // to detect mutable vars that are never changed
|
2019-12-24 20:54:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:02:54 +03:00
|
|
|
pub struct GlobalDecl {
|
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
name string
|
|
|
|
expr Expr
|
2020-04-07 19:51:39 +03:00
|
|
|
has_expr bool
|
2020-05-16 17:12:23 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-09 16:33:46 +03:00
|
|
|
typ table.Type
|
2020-02-03 09:02:54 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:10:46 +03:00
|
|
|
pub struct File {
|
2019-12-24 20:54:43 +03:00
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
path string
|
|
|
|
mod Module
|
|
|
|
stmts []Stmt
|
|
|
|
scope &Scope
|
2020-04-04 06:14:40 +03:00
|
|
|
global_scope &Scope
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-27 23:53:26 +03:00
|
|
|
imports []Import
|
2020-05-18 19:06:09 +03:00
|
|
|
errors []errors.Error
|
2020-05-10 12:26:57 +03:00
|
|
|
warnings []errors.Warning
|
2020-02-17 14:25:18 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:10:46 +03:00
|
|
|
pub struct IdentFn {
|
2020-02-17 14:25:18 +03:00
|
|
|
pub mut:
|
2020-03-11 18:10:46 +03:00
|
|
|
typ table.Type
|
2020-01-19 01:26:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IdentVar {
|
2020-02-06 15:57:35 +03:00
|
|
|
pub mut:
|
2020-03-13 07:57:51 +03:00
|
|
|
typ table.Type
|
|
|
|
is_mut bool
|
|
|
|
is_static bool
|
|
|
|
is_optional bool
|
2020-01-19 01:26:14 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:10:46 +03:00
|
|
|
pub type IdentInfo = IdentFn | IdentVar
|
2020-01-19 01:26:14 +03:00
|
|
|
|
|
|
|
pub enum IdentKind {
|
2020-02-17 14:25:18 +03:00
|
|
|
unresolved
|
2020-01-19 01:26:14 +03:00
|
|
|
blank_ident
|
|
|
|
variable
|
2020-02-03 09:02:54 +03:00
|
|
|
constant
|
2020-04-04 06:14:40 +03:00
|
|
|
global
|
2020-02-17 14:25:18 +03:00
|
|
|
function
|
2019-12-24 20:54:43 +03:00
|
|
|
}
|
2019-12-28 16:11:05 +03:00
|
|
|
|
2019-12-22 04:34:37 +03:00
|
|
|
// A single identifier
|
2019-12-28 16:11:05 +03:00
|
|
|
pub struct Ident {
|
|
|
|
pub:
|
2020-02-10 10:32:08 +03:00
|
|
|
value string
|
2020-05-19 18:12:47 +03:00
|
|
|
language table.Language
|
2019-12-31 12:53:30 +03:00
|
|
|
tok_kind token.Kind
|
2020-04-04 06:14:40 +03:00
|
|
|
mod string
|
2020-01-19 01:26:14 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-06 02:10:01 +03:00
|
|
|
name string
|
2020-01-19 01:26:14 +03:00
|
|
|
kind IdentKind
|
|
|
|
info IdentInfo
|
2020-04-27 23:53:26 +03:00
|
|
|
is_mut bool
|
2019-12-22 04:34:37 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 16:49:14 +03:00
|
|
|
pub fn (i &Ident) var_info() IdentVar {
|
2020-02-06 19:38:02 +03:00
|
|
|
match i.info {
|
|
|
|
IdentVar {
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// return IdentVar{}
|
|
|
|
panic('Ident.var_info(): info is not IdentVar variant')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 23:34:38 +03:00
|
|
|
pub struct InfixExpr {
|
2019-12-22 04:34:37 +03:00
|
|
|
pub:
|
2020-01-22 23:34:38 +03:00
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
|
|
|
left Expr
|
|
|
|
right Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-07 02:34:14 +03:00
|
|
|
left_type table.Type
|
2020-02-10 10:32:08 +03:00
|
|
|
right_type table.Type
|
2019-12-22 04:34:37 +03:00
|
|
|
}
|
|
|
|
|
2020-01-06 18:13:12 +03:00
|
|
|
pub struct PostfixExpr {
|
|
|
|
pub:
|
|
|
|
op token.Kind
|
|
|
|
expr Expr
|
2020-02-04 09:37:38 +03:00
|
|
|
pos token.Position
|
2020-01-06 18:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PrefixExpr {
|
|
|
|
pub:
|
|
|
|
op token.Kind
|
|
|
|
right Expr
|
2020-03-28 16:38:16 +03:00
|
|
|
pos token.Position
|
2020-01-06 18:13:12 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 14:14:10 +03:00
|
|
|
pub struct IndexExpr {
|
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
pos token.Position
|
|
|
|
left Expr
|
|
|
|
index Expr // [0], [start..end] etc
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-03 11:41:01 +03:00
|
|
|
left_type table.Type // array, map, fixed array
|
2020-04-09 16:33:46 +03:00
|
|
|
is_setter bool
|
2020-01-07 14:14:10 +03:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:16:04 +03:00
|
|
|
pub struct IfExpr {
|
2019-12-29 10:51:55 +03:00
|
|
|
pub:
|
2020-03-20 16:39:56 +03:00
|
|
|
tok_kind token.Kind
|
|
|
|
branches []IfBranch
|
|
|
|
left Expr // `a` in `a := if ...`
|
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-20 16:39:56 +03:00
|
|
|
is_expr bool
|
|
|
|
typ table.Type
|
|
|
|
has_else bool
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IfBranch {
|
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
|
|
|
pos token.Position
|
2020-04-07 16:15:45 +03:00
|
|
|
comment Comment
|
2020-02-03 09:02:54 +03:00
|
|
|
}
|
|
|
|
|
2020-02-05 12:00:11 +03:00
|
|
|
pub struct MatchExpr {
|
|
|
|
pub:
|
2020-03-18 18:07:52 +03:00
|
|
|
tok_kind token.Kind
|
|
|
|
cond Expr
|
|
|
|
branches []MatchBranch
|
|
|
|
pos token.Position
|
2020-04-09 16:33:46 +03:00
|
|
|
is_mut bool // `match mut ast_node {`
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-18 18:07:52 +03:00
|
|
|
is_expr bool // returns a value
|
|
|
|
return_type table.Type
|
|
|
|
cond_type table.Type // type of `x` in `match x {`
|
|
|
|
expected_type table.Type // for debugging only
|
|
|
|
is_sum_type bool
|
2020-05-31 11:22:18 +03:00
|
|
|
is_interface bool
|
2020-02-05 12:00:11 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 13:59:45 +03:00
|
|
|
pub struct MatchBranch {
|
|
|
|
pub:
|
2020-04-14 16:46:58 +03:00
|
|
|
exprs []Expr // left side
|
|
|
|
stmts []Stmt // right side
|
2020-04-09 16:33:46 +03:00
|
|
|
pos token.Position
|
2020-04-07 16:15:45 +03:00
|
|
|
comment Comment // comment above `xxx {`
|
2020-04-14 02:03:31 +03:00
|
|
|
is_else bool
|
2020-03-04 13:59:45 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:52:44 +03:00
|
|
|
/*
|
2020-04-25 17:34:42 +03:00
|
|
|
CompIf.is_opt:
|
2020-04-23 17:52:44 +03:00
|
|
|
`$if xyz? {}` => this compile time `if` is optional,
|
|
|
|
and .is_opt reflects the presence of ? at the end.
|
|
|
|
When .is_opt is true, the code should compile, even
|
|
|
|
if `xyz` is NOT defined.
|
|
|
|
If .is_opt is false, then when `xyz` is not defined,
|
|
|
|
the compilation will fail.
|
|
|
|
*/
|
2020-04-25 17:34:42 +03:00
|
|
|
pub struct CompIf {
|
|
|
|
pub:
|
|
|
|
val string
|
|
|
|
stmts []Stmt
|
|
|
|
is_not bool
|
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-23 17:52:44 +03:00
|
|
|
is_opt bool
|
2020-03-22 16:54:31 +03:00
|
|
|
has_else bool
|
2020-02-03 09:02:54 +03:00
|
|
|
else_stmts []Stmt
|
2019-12-22 04:34:37 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 08:16:59 +03:00
|
|
|
pub struct ForStmt {
|
|
|
|
pub:
|
2020-02-19 13:06:36 +03:00
|
|
|
cond Expr
|
|
|
|
stmts []Stmt
|
|
|
|
is_inf bool // `for {}`
|
2020-03-18 16:50:21 +03:00
|
|
|
pos token.Position
|
2020-01-07 02:14:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ForInStmt {
|
|
|
|
pub:
|
2020-03-24 09:25:10 +03:00
|
|
|
key_var string
|
|
|
|
val_var string
|
|
|
|
cond Expr
|
|
|
|
is_range bool
|
|
|
|
high Expr // `10` in `for i in 0..10 {`
|
|
|
|
stmts []Stmt
|
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-24 09:25:10 +03:00
|
|
|
key_type table.Type
|
|
|
|
val_type table.Type
|
|
|
|
cond_type table.Type
|
|
|
|
kind table.Kind // array/map/string
|
2020-01-07 02:14:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ForCStmt {
|
|
|
|
pub:
|
2020-03-11 04:44:30 +03:00
|
|
|
init Stmt // i := 0;
|
|
|
|
has_init bool
|
|
|
|
cond Expr // i < 10;
|
2020-04-10 10:00:14 +03:00
|
|
|
has_cond bool
|
2020-03-11 04:44:30 +03:00
|
|
|
inc Expr // i++;
|
2020-04-10 10:00:14 +03:00
|
|
|
has_inc bool
|
2020-03-11 04:44:30 +03:00
|
|
|
stmts []Stmt
|
2020-03-18 16:50:21 +03:00
|
|
|
pos token.Position
|
2019-12-30 08:16:59 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 01:09:59 +03:00
|
|
|
/*
|
2019-12-28 21:16:04 +03:00
|
|
|
pub struct ReturnStmt {
|
2020-03-18 17:39:26 +03:00
|
|
|
pub:
|
2019-12-31 12:53:30 +03:00
|
|
|
tok_kind token.Kind // or pos
|
2019-12-28 11:43:22 +03:00
|
|
|
results []Expr
|
2020-03-18 16:50:21 +03:00
|
|
|
pos token.Position
|
2019-12-22 04:34:37 +03:00
|
|
|
}
|
2020-05-12 01:09:59 +03:00
|
|
|
*/
|
2020-02-04 11:54:15 +03:00
|
|
|
// #include etc
|
|
|
|
pub struct HashStmt {
|
|
|
|
pub:
|
2020-03-05 02:43:02 +03:00
|
|
|
val string
|
2020-04-16 12:29:36 +03:00
|
|
|
mod string
|
2020-02-04 11:54:15 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:42:57 +03:00
|
|
|
// filter(), map()
|
|
|
|
pub struct Lambda {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2019-12-28 21:16:04 +03:00
|
|
|
pub struct AssignStmt {
|
|
|
|
pub:
|
2020-05-27 04:20:22 +03:00
|
|
|
right []Expr
|
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
2020-04-27 23:53:26 +03:00
|
|
|
pub mut:
|
2020-05-27 04:20:22 +03:00
|
|
|
left []Ident
|
|
|
|
left_types []table.Type
|
|
|
|
right_types []table.Type
|
|
|
|
is_static bool // for translated code only
|
2020-05-26 19:00:51 +03:00
|
|
|
has_cross_var bool
|
2019-12-28 21:16:04 +03:00
|
|
|
}
|
2020-02-06 19:38:02 +03:00
|
|
|
|
2020-03-02 21:00:33 +03:00
|
|
|
pub struct AsCast {
|
|
|
|
pub:
|
2020-03-18 15:55:46 +03:00
|
|
|
expr Expr
|
|
|
|
typ table.Type
|
2020-03-18 16:50:21 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-18 15:55:46 +03:00
|
|
|
expr_type table.Type
|
2020-03-02 21:00:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:02:54 +03:00
|
|
|
// e.g. `[unsafe_fn]`
|
|
|
|
pub struct Attr {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
}
|
2019-12-28 21:16:04 +03:00
|
|
|
|
2020-02-11 01:19:50 +03:00
|
|
|
pub struct EnumVal {
|
2020-02-10 22:33:34 +03:00
|
|
|
pub:
|
2020-02-25 17:02:34 +03:00
|
|
|
enum_name string
|
|
|
|
val string
|
2020-03-15 02:46:08 +03:00
|
|
|
mod string // for full path `mod_Enum_val`
|
2020-02-25 17:02:34 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-15 04:51:31 +03:00
|
|
|
typ table.Type
|
2020-02-11 01:19:50 +03:00
|
|
|
}
|
2020-02-10 22:33:34 +03:00
|
|
|
|
2020-04-09 20:23:49 +03:00
|
|
|
pub struct EnumField {
|
2020-05-18 19:06:09 +03:00
|
|
|
pub:
|
2020-04-10 15:44:01 +03:00
|
|
|
name string
|
|
|
|
pos token.Position
|
|
|
|
expr Expr
|
|
|
|
has_expr bool
|
2020-04-09 20:23:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-11 01:19:50 +03:00
|
|
|
pub struct EnumDecl {
|
|
|
|
pub:
|
2020-05-27 04:20:22 +03:00
|
|
|
name string
|
|
|
|
is_pub bool
|
2020-05-26 00:00:48 +03:00
|
|
|
is_flag bool // true when the enum has [flag] tag
|
2020-05-27 04:20:22 +03:00
|
|
|
fields []EnumField
|
|
|
|
pos token.Position
|
2020-02-11 01:19:50 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:37:55 +03:00
|
|
|
pub struct AliasTypeDecl {
|
|
|
|
pub:
|
2020-03-08 00:37:03 +03:00
|
|
|
name string
|
|
|
|
is_pub bool
|
2020-03-07 19:37:55 +03:00
|
|
|
parent_type table.Type
|
2020-04-19 01:07:57 +03:00
|
|
|
pos token.Position
|
2020-03-07 19:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SumTypeDecl {
|
2020-02-11 01:19:50 +03:00
|
|
|
pub:
|
2020-03-08 00:37:03 +03:00
|
|
|
name string
|
|
|
|
is_pub bool
|
2020-03-07 19:37:55 +03:00
|
|
|
sub_types []table.Type
|
2020-04-19 01:07:57 +03:00
|
|
|
pos token.Position
|
2020-02-10 22:33:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:10:46 +03:00
|
|
|
pub struct FnTypeDecl {
|
|
|
|
pub:
|
2020-03-11 21:00:51 +03:00
|
|
|
name string
|
|
|
|
is_pub bool
|
|
|
|
typ table.Type
|
2020-04-19 01:07:57 +03:00
|
|
|
pos token.Position
|
2020-03-11 18:10:46 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:33:46 +03:00
|
|
|
// TODO: handle this differently
|
|
|
|
// v1 excludes non current os ifdefs so
|
|
|
|
// the defer's never get added in the first place
|
2020-02-18 00:50:04 +03:00
|
|
|
pub struct DeferStmt {
|
2020-02-11 12:26:46 +03:00
|
|
|
pub:
|
2020-02-18 00:50:04 +03:00
|
|
|
stmts []Stmt
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-27 09:21:22 +03:00
|
|
|
ifdef string
|
2020-02-11 12:26:46 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:45:03 +03:00
|
|
|
pub struct UnsafeStmt {
|
|
|
|
pub:
|
|
|
|
stmts []Stmt
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:41:19 +03:00
|
|
|
// `(3+4)`
|
|
|
|
pub struct ParExpr {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:13:12 +03:00
|
|
|
pub struct AssignExpr {
|
|
|
|
pub:
|
2020-03-16 09:42:45 +03:00
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
|
|
|
left Expr
|
|
|
|
val Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-16 09:42:45 +03:00
|
|
|
left_type table.Type
|
|
|
|
right_type table.Type
|
2020-01-06 18:13:12 +03:00
|
|
|
}
|
|
|
|
|
2020-03-16 10:33:42 +03:00
|
|
|
pub struct GoStmt {
|
|
|
|
pub:
|
2020-04-03 16:18:17 +03:00
|
|
|
call_expr Expr
|
2020-03-16 10:33:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 00:50:04 +03:00
|
|
|
pub struct GotoLabel {
|
2020-02-17 16:15:42 +03:00
|
|
|
pub:
|
2020-02-18 00:50:04 +03:00
|
|
|
name string
|
2020-02-17 16:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GotoStmt {
|
|
|
|
pub:
|
2020-02-18 00:50:04 +03:00
|
|
|
name string
|
2020-02-17 16:15:42 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 11:38:12 +03:00
|
|
|
pub struct ArrayInit {
|
|
|
|
pub:
|
2020-04-29 13:20:22 +03:00
|
|
|
pos token.Position
|
2020-05-31 11:22:18 +03:00
|
|
|
elem_type_pos token.Position
|
2020-04-29 13:20:22 +03:00
|
|
|
exprs []Expr
|
|
|
|
is_fixed bool
|
|
|
|
has_val bool
|
|
|
|
mod string
|
|
|
|
len_expr Expr
|
2020-05-13 17:11:52 +03:00
|
|
|
cap_expr Expr
|
|
|
|
default_expr Expr
|
2020-04-29 13:20:22 +03:00
|
|
|
has_len bool
|
|
|
|
has_cap bool
|
2020-05-13 17:11:52 +03:00
|
|
|
has_default bool
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-29 13:20:22 +03:00
|
|
|
is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]`
|
|
|
|
interface_types []table.Type // [Dog, Cat]
|
|
|
|
interface_type table.Type // Animal
|
|
|
|
elem_type table.Type
|
|
|
|
typ table.Type
|
2019-12-30 11:38:12 +03:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:13:19 +03:00
|
|
|
pub struct MapInit {
|
|
|
|
pub:
|
2020-03-07 18:23:10 +03:00
|
|
|
pos token.Position
|
|
|
|
keys []Expr
|
|
|
|
vals []Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-07 18:23:10 +03:00
|
|
|
typ table.Type
|
2020-03-07 10:13:00 +03:00
|
|
|
key_type table.Type
|
|
|
|
value_type table.Type
|
2020-02-22 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:31:54 +03:00
|
|
|
// s[10..20]
|
|
|
|
pub struct RangeExpr {
|
|
|
|
pub:
|
2020-03-07 00:24:39 +03:00
|
|
|
low Expr
|
|
|
|
high Expr
|
|
|
|
has_high bool
|
|
|
|
has_low bool
|
2020-02-02 16:31:54 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:43:17 +03:00
|
|
|
pub struct CastExpr {
|
|
|
|
pub:
|
2020-03-11 21:00:51 +03:00
|
|
|
expr Expr // `buf`
|
|
|
|
arg Expr // `n` in `string(buf, n)`
|
|
|
|
typ table.Type // `string`
|
2020-05-06 13:05:24 +03:00
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-04-27 23:53:26 +03:00
|
|
|
typname string
|
2020-03-11 21:00:51 +03:00
|
|
|
expr_type table.Type // `byteptr`
|
|
|
|
has_arg bool
|
2020-02-10 16:43:17 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 17:51:05 +03:00
|
|
|
pub struct AssertStmt {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
2020-03-26 12:44:59 +03:00
|
|
|
pos token.Position
|
2020-02-26 17:51:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:36:41 +03:00
|
|
|
// `if [x := opt()] {`
|
|
|
|
pub struct IfGuardExpr {
|
2020-02-27 20:02:40 +03:00
|
|
|
pub:
|
2020-03-17 18:40:41 +03:00
|
|
|
var_name string
|
|
|
|
expr Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-17 18:40:41 +03:00
|
|
|
expr_type table.Type
|
2020-02-27 20:02:40 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:51:15 +03:00
|
|
|
pub enum OrKind {
|
|
|
|
absent
|
|
|
|
block
|
|
|
|
propagate
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:36:41 +03:00
|
|
|
// `or { ... }`
|
|
|
|
pub struct OrExpr {
|
|
|
|
pub:
|
2020-05-23 09:51:15 +03:00
|
|
|
stmts []Stmt
|
|
|
|
kind OrKind
|
2020-05-25 06:32:33 +03:00
|
|
|
pos token.Position
|
2020-02-28 17:36:41 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 19:29:47 +03:00
|
|
|
pub struct Assoc {
|
|
|
|
pub:
|
2020-03-01 15:07:51 +03:00
|
|
|
var_name string
|
|
|
|
fields []string
|
|
|
|
exprs []Expr
|
|
|
|
pos token.Position
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-19 11:52:33 +03:00
|
|
|
typ table.Type
|
2020-02-18 19:29:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 20:13:34 +03:00
|
|
|
pub struct SizeOf {
|
|
|
|
pub:
|
2020-03-18 10:41:49 +03:00
|
|
|
typ table.Type
|
2020-02-18 20:13:34 +03:00
|
|
|
type_name string
|
|
|
|
}
|
|
|
|
|
2020-06-09 17:36:18 +03:00
|
|
|
pub struct Likely {
|
|
|
|
pub:
|
|
|
|
expr Expr
|
|
|
|
pos token.Position
|
2020-06-09 18:08:31 +03:00
|
|
|
is_likely bool // false for _unlikely_
|
2020-06-09 17:36:18 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 14:15:39 +03:00
|
|
|
pub struct TypeOf {
|
|
|
|
pub:
|
2020-03-31 20:43:11 +03:00
|
|
|
expr Expr
|
2020-05-09 16:16:48 +03:00
|
|
|
pub mut:
|
2020-03-28 19:37:22 +03:00
|
|
|
expr_type table.Type
|
2020-03-19 14:15:39 +03:00
|
|
|
}
|
|
|
|
|
2020-04-05 03:08:10 +03:00
|
|
|
pub struct Comment {
|
2020-02-18 22:20:15 +03:00
|
|
|
pub:
|
2020-04-09 16:33:46 +03:00
|
|
|
text string
|
2020-04-05 03:08:10 +03:00
|
|
|
is_multi bool
|
2020-04-09 16:33:46 +03:00
|
|
|
line_nr int
|
|
|
|
pos token.Position
|
2020-02-18 22:20:15 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 16:57:54 +03:00
|
|
|
pub struct ConcatExpr {
|
|
|
|
pub:
|
2020-05-18 19:06:09 +03:00
|
|
|
vals []Expr
|
2020-05-16 00:14:53 +03:00
|
|
|
pub mut:
|
|
|
|
return_type table.Type
|
2020-03-01 16:57:54 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 06:32:33 +03:00
|
|
|
pub struct ComptimeCall {
|
2020-06-06 11:05:26 +03:00
|
|
|
pub:
|
|
|
|
method_name string
|
|
|
|
left Expr
|
2020-06-06 22:36:24 +03:00
|
|
|
is_vweb bool
|
2020-06-07 13:26:45 +03:00
|
|
|
// vweb_stmts []Stmt
|
|
|
|
vweb_tmpl File
|
2020-06-06 11:05:26 +03:00
|
|
|
pub mut:
|
|
|
|
sym table.TypeSymbol
|
2020-05-25 06:32:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 21:54:36 +03:00
|
|
|
pub struct None {
|
|
|
|
pub:
|
2020-06-15 08:10:45 +03:00
|
|
|
pos token.Position
|
|
|
|
foo int // todo
|
2020-02-19 21:54:36 +03:00
|
|
|
}
|
2020-03-18 10:41:49 +03:00
|
|
|
|
2020-03-18 03:19:23 +03:00
|
|
|
[inline]
|
|
|
|
pub fn expr_is_blank_ident(expr Expr) bool {
|
|
|
|
match expr {
|
2020-04-19 01:07:57 +03:00
|
|
|
Ident { return it.kind == .blank_ident }
|
|
|
|
else { return false }
|
2020-03-18 03:19:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:59:55 +03:00
|
|
|
pub fn (expr Expr) position() token.Position {
|
2020-04-17 03:38:39 +03:00
|
|
|
// all uncommented have to be implemented
|
2020-04-22 21:20:49 +03:00
|
|
|
match mut expr {
|
|
|
|
ArrayInit {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
AsCast {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.Ident { }
|
2020-04-22 21:20:49 +03:00
|
|
|
AssignExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-05-06 13:05:24 +03:00
|
|
|
CastExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-22 21:20:49 +03:00
|
|
|
Assoc {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
BoolLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
CallExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
CharLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
EnumVal {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
FloatLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
Ident {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
IfExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.IfGuardExpr { }
|
2020-04-22 21:20:49 +03:00
|
|
|
IndexExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
InfixExpr {
|
|
|
|
left_pos := it.left.position()
|
|
|
|
right_pos := it.right.position()
|
|
|
|
if left_pos.pos == 0 || right_pos.pos == 0 {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
return token.Position{
|
|
|
|
line_nr: it.pos.line_nr
|
|
|
|
pos: left_pos.pos
|
|
|
|
len: right_pos.pos - left_pos.pos + right_pos.len
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 21:20:49 +03:00
|
|
|
IntegerLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
MapInit {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
MatchExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-06-15 08:10:45 +03:00
|
|
|
None {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-22 21:20:49 +03:00
|
|
|
PostfixExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.None { }
|
2020-04-22 21:20:49 +03:00
|
|
|
PrefixExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.ParExpr { }
|
2020-04-22 21:20:49 +03:00
|
|
|
SelectorExpr {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.SizeOf { }
|
2020-04-22 21:20:49 +03:00
|
|
|
StringLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
|
|
|
StringInterLiteral {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.Type { }
|
2020-04-22 21:20:49 +03:00
|
|
|
StructInit {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-06-09 17:36:18 +03:00
|
|
|
Likely {
|
|
|
|
return it.pos
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
// ast.TypeOf { }
|
2020-04-22 21:20:49 +03:00
|
|
|
else {
|
|
|
|
return token.Position{}
|
|
|
|
}
|
2020-04-17 03:38:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 13:31:18 +03:00
|
|
|
|
2020-05-11 09:59:55 +03:00
|
|
|
pub fn (stmt Stmt) position() token.Position {
|
2020-05-25 06:32:33 +03:00
|
|
|
match stmt {
|
2020-05-18 19:06:09 +03:00
|
|
|
AssertStmt { return it.pos }
|
|
|
|
AssignStmt { return it.pos }
|
|
|
|
/*
|
2020-04-29 13:31:18 +03:00
|
|
|
// Attr {
|
|
|
|
// }
|
|
|
|
// Block {
|
|
|
|
// }
|
|
|
|
// BranchStmt {
|
|
|
|
// }
|
2020-05-18 19:06:09 +03:00
|
|
|
*/
|
|
|
|
Comment { return it.pos }
|
|
|
|
CompIf { return it.pos }
|
|
|
|
ConstDecl { return it.pos }
|
|
|
|
/*
|
2020-04-29 13:31:18 +03:00
|
|
|
// DeferStmt {
|
|
|
|
// }
|
2020-05-18 19:06:09 +03:00
|
|
|
*/
|
|
|
|
EnumDecl { return it.pos }
|
|
|
|
ExprStmt { return it.pos }
|
|
|
|
FnDecl { return it.pos }
|
|
|
|
ForCStmt { return it.pos }
|
|
|
|
ForInStmt { return it.pos }
|
|
|
|
ForStmt { return it.pos }
|
|
|
|
/*
|
2020-04-29 13:31:18 +03:00
|
|
|
// GlobalDecl {
|
|
|
|
// }
|
|
|
|
// GoStmt {
|
|
|
|
// }
|
|
|
|
// GotoLabel {
|
|
|
|
// }
|
|
|
|
// GotoStmt {
|
|
|
|
// }
|
|
|
|
// HashStmt {
|
|
|
|
// }
|
2020-05-18 19:06:09 +03:00
|
|
|
*/
|
|
|
|
Import { return it.pos }
|
|
|
|
/*
|
2020-04-29 13:31:18 +03:00
|
|
|
// InterfaceDecl {
|
|
|
|
// }
|
|
|
|
// Module {
|
|
|
|
// }
|
2020-05-18 19:06:09 +03:00
|
|
|
*/
|
|
|
|
Return { return it.pos }
|
|
|
|
StructDecl { return it.pos }
|
|
|
|
/*
|
2020-04-29 13:31:18 +03:00
|
|
|
// TypeDecl {
|
|
|
|
// }
|
|
|
|
// UnsafeStmt {
|
|
|
|
// }
|
2020-05-18 19:06:09 +03:00
|
|
|
*/
|
|
|
|
else { return token.Position{} }
|
2020-04-29 13:31:18 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 22:51:55 +03:00
|
|
|
|
|
|
|
// TODO: remove this fugly hack :-|
|
2020-05-03 22:13:40 +03:00
|
|
|
// fe2ex/1 and ex2fe/1 are used to convert back and forth from
|
2020-05-02 22:51:55 +03:00
|
|
|
// table.FExpr to ast.Expr , which in turn is needed to break
|
|
|
|
// a dependency cycle between v.ast and v.table, for the single
|
|
|
|
// field table.Field.default_expr, which should be ast.Expr
|
|
|
|
pub fn fe2ex(x table.FExpr) Expr {
|
|
|
|
res := Expr{}
|
|
|
|
C.memcpy(&res, &x, sizeof(Expr))
|
|
|
|
return res
|
|
|
|
}
|
2020-05-18 19:06:09 +03:00
|
|
|
|
2020-05-02 22:51:55 +03:00
|
|
|
pub fn ex2fe(x Expr) table.FExpr {
|
|
|
|
res := table.FExpr{}
|
|
|
|
C.memcpy(&res, &x, sizeof(table.FExpr))
|
|
|
|
return res
|
|
|
|
}
|