v/vlib/v/ast/ast.v

566 lines
7.4 KiB
V
Raw Normal View History

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 (
v.token
v.table
2019-12-22 02:34:37 +01:00
)
2020-02-18 20:47:19 +01:00
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr
2019-12-28 14:11:05 +01:00
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
2020-02-26 20:45:03 +01:00
LineComment | MultiLineComment | AssertStmt | UnsafeStmt
2020-02-17 22:50:04 +01:00
pub type Type = StructType | ArrayType
pub struct StructType {
fields []Field
}
pub struct ArrayType {}
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
typ 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 {
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
}
pub struct BoolLiteral {
pub:
val bool
}
// `foo.bar`
pub struct SelectorExpr {
pub:
pos token.Position
expr Expr
field string
}
2019-12-28 11:02:06 +01:00
// module declaration
pub struct Module {
pub:
2019-12-28 09:43:22 +01:00
name string
path string
expr Expr
}
pub struct Field {
pub:
name string
// type_idx int
mut:
typ table.Type
2020-02-17 22:50:04 +01:00
// typ2 Type
}
pub struct ConstDecl {
pub:
fields []Field
exprs []Expr
is_pub bool
}
pub struct StructDecl {
pub:
2020-02-22 14:13:19 +01:00
pos token.Position
name string
fields []Field
is_pub bool
mut_pos int // mut:
pub_pos int // pub:
pub_mut_pos int // pub mut:
}
pub struct StructInit {
pub:
pos token.Position
typ table.Type
fields []string
exprs []Expr
}
// import statement
pub struct Import {
pub:
pos token.Position
mod string
alias string
2019-12-31 19:42:16 +01:00
// expr Expr
}
2019-12-29 07:24:17 +01:00
pub struct Arg {
pub:
typ table.Type
2019-12-29 07:24:17 +01:00
name string
}
2019-12-27 13:57:49 +01:00
pub struct FnDecl {
pub:
name string
stmts []Stmt
typ table.Type
args []Arg
is_pub bool
is_variadic bool
receiver Field
2020-02-18 20:20:15 +01:00
is_method bool
rec_mut bool // is receiver mutable
2019-12-29 07:24:17 +01:00
}
pub struct BranchStmt {
pub:
tok token.Token
}
2019-12-29 07:24:17 +01:00
pub struct CallExpr {
pub:
// tok token.Token
pos token.Position
mut:
// func Expr
name string
args []Expr
2020-02-18 18:13:34 +01:00
is_c bool
2020-02-27 21:00:33 +01:00
muts []bool
}
pub struct MethodCallExpr {
pub:
// tok token.Token
pos token.Position
expr Expr
name string
args []Expr
muts []bool
or_block OrExpr
2019-12-27 13:57:49 +01:00
}
pub struct Return {
pub:
pos token.Position
expected_type 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 {
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:
name string
2020-02-27 21:51:40 +01:00
name2 string // TODO
expr Expr
is_mut bool
mut:
typ table.Type
pos token.Position
2019-12-24 18:54:43 +01:00
}
pub struct GlobalDecl {
pub:
name string
expr Expr
mut:
typ table.Type
}
2020-02-07 14:49:14 +01:00
pub struct StmtBlock {
pub:
stmts []Stmt
}
2019-12-30 12:10:46 +01:00
pub struct File {
2019-12-24 18:54:43 +01:00
pub:
2020-02-17 22:50:04 +01:00
path string
mod Module
imports []Import
stmts []Stmt
scope &Scope
}
pub struct IdentFunc {
pub mut:
return_type table.Type
}
pub struct IdentVar {
pub mut:
typ table.Type
is_mut bool
is_static bool
}
type IdentInfo = IdentFunc | IdentVar
pub enum IdentKind {
unresolved
blank_ident
variable
constant
function
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
value string
is_c bool
tok_kind token.Kind
pos token.Position
mut:
kind IdentKind
info IdentInfo
2019-12-22 02:34:37 +01:00
}
2020-02-07 14:49:14 +01:00
pub fn (i &Ident) var_info() IdentVar {
2020-02-06 17:38:02 +01:00
match i.info {
IdentVar {
return it
}
else {
// return IdentVar{}
panic('Ident.var_info(): info is not IdentVar variant')
}
}
}
pub struct InfixExpr {
2019-12-22 02:34:37 +01:00
pub:
2019-12-28 19:16:04 +01:00
// op BinaryOp
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
}
/*
// renamed to PrefixExpr
pub struct UnaryExpr {
pub:
// tok_kind token.Kind
2019-12-28 09:43:22 +01:00
// op BinaryOp
op token.Kind
2019-12-28 09:43:22 +01:00
left Expr
}
*/
2020-02-07 14:49:14 +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
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 {
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
left Expr // `a` in `a := if ...`
pos token.Position
mut:
typ table.Type
2020-02-22 16:59:50 +01:00
has_else bool
}
pub struct MatchExpr {
pub:
2020-02-07 14:49:14 +01:00
tok_kind token.Kind
cond Expr
blocks []StmtBlock
match_exprs []Expr
pos token.Position
mut:
typ table.Type
}
pub struct CompIf {
pub:
cond Expr
stmts []Stmt
else_stmts []Stmt
2019-12-22 02:34:37 +01:00
}
pub struct ForStmt {
pub:
2020-02-19 11:06:36 +01:00
cond Expr
stmts []Stmt
pos token.Position
is_inf bool // `for {}`
2020-01-07 00:14:19 +01:00
}
pub struct ForInStmt {
pub:
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-01-07 00:14:19 +01:00
}
pub struct ForCStmt {
pub:
init Stmt // i := 0;
cond Expr // i < 10;
inc Stmt // i++;
stmts []Stmt
}
2019-12-28 19:16:04 +01:00
pub struct ReturnStmt {
tok_kind token.Kind // or pos
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-02-10 14:42:57 +01:00
// filter(), map()
pub struct Lambda {
pub:
name string
}
2019-12-28 19:16:04 +01:00
pub struct AssignStmt {
pub:
2020-02-06 17:38:02 +01:00
left []Ident
right []Expr
op token.Kind
pos token.Position
2019-12-28 19:16:04 +01:00
}
2020-02-06 17:38:02 +01:00
// e.g. `[unsafe_fn]`
pub struct Attr {
pub:
name string
}
2019-12-28 19:16:04 +01:00
pub struct EnumVal {
pub:
2020-02-25 15:02:34 +01:00
enum_name string
val string
pos token.Position
// name string
}
pub struct EnumDecl {
pub:
name string
is_pub bool
vals []string
}
2020-02-17 22:50:04 +01:00
pub struct TypeDecl {
pub:
name string
is_pub bool
}
2020-02-17 22:50:04 +01:00
pub struct DeferStmt {
2020-02-11 10:26:46 +01:00
pub:
2020-02-17 22:50:04 +01:00
stmts []Stmt
2020-02-11 10:26:46 +01:00
}
2020-02-26 20:45:03 +01:00
pub struct UnsafeStmt {
pub:
stmts []Stmt
}
2020-02-28 14:41:19 +01:00
// `(3+4)`
pub struct ParExpr {
pub:
expr Expr
}
2020-01-06 16:13:12 +01:00
pub struct AssignExpr {
pub:
op token.Kind
pos token.Position
2020-01-06 16:13:12 +01:00
left Expr
val Expr
}
2020-02-17 22:50:04 +01:00
pub struct GotoLabel {
2020-02-17 14:15:42 +01:00
pub:
2020-02-17 22:50:04 +01:00
name string
2020-02-17 14:15:42 +01:00
}
pub struct GotoStmt {
pub:
2020-02-17 22:50:04 +01:00
name string
2020-02-17 14:15:42 +01:00
}
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
mut:
typ table.Type
2019-12-30 09:38:12 +01:00
}
2020-02-22 14:13:19 +01:00
pub struct MapInit {
pub:
pos token.Position
keys []Expr
vals []Expr
mut:
typ table.Type
}
2020-02-02 14:31:54 +01:00
// s[10..20]
pub struct RangeExpr {
pub:
low Expr
high Expr
}
pub struct CastExpr {
pub:
typ table.Type
expr Expr
}
pub struct AssertStmt {
pub:
expr Expr
}
// `if [x := opt()] {`
pub struct IfGuardExpr {
pub:
var_name string
expr Expr
}
// `or { ... }`
pub struct OrExpr {
pub:
stmts []Stmt
// var_name string
// expr Expr
}
pub struct Assoc {
pub:
2020-02-22 14:13:19 +01:00
name string
fields []string
exprs []Expr
}
2020-02-18 18:13:34 +01:00
pub struct SizeOf {
pub:
type_name string
}
2020-02-18 20:20:15 +01:00
pub struct LineComment {
pub:
text string
}
pub struct MultiLineComment {
pub:
text string
}
2020-02-19 19:54:36 +01:00
pub struct None {
pub:
foo int // todo
}
/*
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
}
*/