v/vlib/v/ast/ast.v

294 lines
3.8 KiB
V
Raw Normal View History

2019-12-22 02:34:37 +01:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module ast
import (
v.token
2019-12-27 08:52:20 +01:00
v.types
2019-12-22 02:34:37 +01:00
)
2020-01-06 16:13:12 +01:00
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr
2019-12-28 14:11:05 +01:00
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl
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
ti types.TypeIdent
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
pub struct BoolLiteral {
pub:
val bool
}
// `foo.bar`
pub struct SelectorExpr {
pub:
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
ti types.TypeIdent
}
pub struct StructDecl {
pub:
name string
fields []Field
2019-12-31 19:42:16 +01:00
is_pub bool
}
pub struct StructInit {
pub:
// typ types.TypeIdent
2020-01-06 16:13:12 +01:00
ti types.TypeIdent
fields []string
exprs []Expr
}
// import statement
pub struct Import {
pub:
2020-01-06 16:13:12 +01:00
mods map[string]string // alias -> module
2019-12-31 19:42:16 +01:00
// expr Expr
}
2019-12-29 07:24:17 +01:00
pub struct Arg {
pub:
ti types.TypeIdent
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
ti types.TypeIdent
args []Arg
is_pub bool
receiver Field
2019-12-29 07:24:17 +01:00
}
pub struct CallExpr {
pub:
name string
args []Expr
is_unknown bool
2020-01-02 08:30:15 +01:00
tok token.Token
2019-12-27 13:57:49 +01:00
}
pub struct Return {
pub:
expr Expr
}
2019-12-22 02:34:37 +01:00
/*
pub enum Expr {
Binary(BinaryExpr)
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
expr Expr
ti types.TypeIdent
2019-12-24 18:54:43 +01:00
}
2019-12-30 12:10:46 +01:00
pub struct File {
2019-12-24 18:54:43 +01:00
pub:
2019-12-28 14:11:05 +01:00
stmts []Stmt
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
tok_kind token.Kind
2019-12-28 09:43:22 +01:00
value string
2019-12-22 02:34:37 +01:00
}
pub struct BinaryExpr {
pub:
// tok_kind token.Kind
2019-12-28 19:16:04 +01:00
// op BinaryOp
op token.Kind
2019-12-28 19:16:04 +01:00
left Expr
2020-01-06 16:13:12 +01:00
// left_ti types.TypeIdent
2019-12-28 19:16:04 +01:00
right Expr
2020-01-06 16:13:12 +01:00
// right_ti types.TypeIdent
2019-12-22 02:34:37 +01:00
}
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-01-06 16:13:12 +01:00
pub struct PostfixExpr {
pub:
op token.Kind
expr Expr
}
pub struct PrefixExpr {
pub:
op token.Kind
right Expr
}
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
ti types.TypeIdent
left Expr // `a` in `a := if ...`
2019-12-22 02:34:37 +01:00
}
pub struct ForStmt {
pub:
cond Expr
stmts []Stmt
2020-01-01 22:34:46 +01:00
is_in bool
}
2019-12-28 19:16:04 +01:00
pub struct ReturnStmt {
tok_kind token.Kind // or pos
2019-12-28 09:43:22 +01:00
results []Expr
2019-12-22 02:34:37 +01:00
}
/*
2019-12-28 19:16:04 +01:00
pub struct AssignStmt {
pub:
left Expr
right Expr
op token.Kind
2019-12-28 19:16:04 +01:00
}
*/
2019-12-28 19:16:04 +01:00
2020-01-06 16:13:12 +01:00
pub struct AssignExpr {
pub:
left Expr
val Expr
op token.Kind
}
2019-12-30 09:38:12 +01:00
pub struct ArrayInit {
pub:
exprs []Expr
ti types.TypeIdent
2019-12-30 09:38:12 +01:00
}
// string representaiton of expr
pub fn (x Expr) str() string {
match x {
BinaryExpr {
return '(${it.left.str()} $it.op.str() ${it.right.str()})'
}
UnaryExpr {
return it.left.str() + it.op.str()
}
IntegerLiteral {
return it.val.str()
}
IntegerLiteral {
return '"$it.val"'
}
2019-12-28 09:43:22 +01:00
else {
return ''
}
}
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]'
}
}
}
/*
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
}
*/