ast: TypeOf

pull/4072/head
Alexander Medvednikov 2020-03-19 12:15:39 +01:00
parent 79077b0025
commit b290efa394
6 changed files with 30 additions and 8 deletions

View File

@ -14,7 +14,7 @@ pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLitera
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
ConcatExpr | Type | AsCast ConcatExpr | Type | AsCast | TypeOf
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
@ -608,6 +608,11 @@ pub:
type_name string type_name string
} }
pub struct TypeOf {
pub:
expr Expr
}
pub struct LineComment { pub struct LineComment {
pub: pub:
text string text string

View File

@ -741,6 +741,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.Type { ast.Type {
return it.typ return it.typ
} }
ast.TypeOf {
return table.string_type
}
/* /*
ast.UnaryExpr { ast.UnaryExpr {
c.expr(it.left) c.expr(it.left)

View File

@ -887,6 +887,9 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('_type_idx_') g.write('_type_idx_')
g.write(g.typ(it.typ)) g.write(g.typ(it.typ))
} }
ast.TypeOf {
g.write('tos3("TYPEOF_TODO")')
}
else { else {
// #printf("node=%d\n", node.typ); // #printf("node=%d\n", node.typ);
println(term.red('cgen.expr(): bad node ' + typeof(node))) println(term.red('cgen.expr(): bad node ' + typeof(node)))

View File

@ -58,11 +58,10 @@ fn (g mut JsGen) stmt(node ast.Stmt) {
g.writeln(';') g.writeln(';')
} }
ast.AssignStmt { ast.AssignStmt {
if it.left.len > it.right.len { if it.left.len > it.right.len {}
// TODO: multi return // TODO: multi return
}
else { else {
for i,ident in it.left { for i, ident in it.left {
var_info := ident.var_info() var_info := ident.var_info()
var_type_sym := g.table.get_type_symbol(var_info.typ) var_type_sym := g.table.get_type_symbol(var_info.typ)
val := it.right[i] val := it.right[i]
@ -90,6 +89,8 @@ fn (g mut JsGen) stmt(node ast.Stmt) {
} }
ast.ExprStmt { ast.ExprStmt {
g.expr(it.expr) g.expr(it.expr)
}
/*
match it.expr { match it.expr {
// no ; after an if expression // no ; after an if expression
ast.IfExpr {} ast.IfExpr {}
@ -97,7 +98,8 @@ fn (g mut JsGen) stmt(node ast.Stmt) {
g.writeln(';') g.writeln(';')
} }
} }
} */
else { else {
verror('jsgen.stmt(): bad node') verror('jsgen.stmt(): bad node')
} }

View File

@ -765,6 +765,15 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
} }
} }
.key_typeof {
p.next()
p.check(.lpar)
expr := p.expr(0)
p.check(.rpar)
node = ast.TypeOf{
expr: expr
}
}
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }` // Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
.lcbr { .lcbr {
p.next() p.next()

View File

@ -114,7 +114,7 @@ pub enum Kind {
key_switch key_switch
key_true key_true
key_type key_type
// typeof key_typeof
key_orelse key_orelse
key_union key_union
key_pub key_pub
@ -229,7 +229,7 @@ fn build_token_str() []string {
s[Kind.key_import] = 'import' s[Kind.key_import] = 'import'
s[Kind.key_embed] = 'embed' s[Kind.key_embed] = 'embed'
s[Kind.key_unsafe] = 'unsafe' s[Kind.key_unsafe] = 'unsafe'
// Kinds[key_typeof] = 'typeof' s[Kind.key_typeof] = 'typeof'
s[Kind.key_enum] = 'enum' s[Kind.key_enum] = 'enum'
s[Kind.key_interface] = 'interface' s[Kind.key_interface] = 'interface'
s[Kind.key_pub] = 'pub' s[Kind.key_pub] = 'pub'