v/vlib/compiler2/ast/ast.v

142 lines
1.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 (
compiler2.token
)
struct Foo {}
2019-12-24 18:54:43 +01:00
pub type Expr = Foo | IfExpr | BinaryExpr | ScalarExpr | UnaryExpr |
StringLiteral | IntegerExpr
pub type Stmt = Foo | VarDecl
2019-12-22 02:34:37 +01:00
pub struct IntegerExpr {
pub:
val int
}
2019-12-24 18:54:43 +01:00
pub struct StringLiteral {
pub:
val string
}
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
*/
pub struct VarDecl {
pub:
name string
expr Expr
}
pub struct Program {
pub:
exprs []Expr
}
2019-12-22 02:34:37 +01:00
// A single identifier
struct Ident {
token token.Token
value string
}
pub struct BinaryExpr {
pub:
token token.Token
//op BinaryOp
op token.Token
left Expr
right Expr
}
pub struct ScalarExpr {
pub:
token token.Token
// op BinaryOp
// op token.Token
typ token.Token
val string
left Expr
}
pub struct UnaryExpr {
pub:
// token token.Token
//op BinaryOp
op token.Token
left Expr
}
2019-12-22 02:34:37 +01:00
struct IfExpr {
token token.Token
cond Expr
body []Stmt
else_ []Stmt
}
struct ReturnStmt {
token token.Token // or pos
results []Expr
}
// string representaiton of expr
pub fn (x Expr) str() string {
match x {
BinaryExpr {
return '(${it.left.str()}$it.op.str()${it.right.str()})'
}
ScalarExpr {
return '${it.left.str()}$it.val'
}
UnaryExpr {
return '${it.left.str()}$it.op.str()'
}
else { return '' }
}
2019-12-22 02:34:37 +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
}
*/