SymbolTable stores function blocks
This commit is contained in:
33
parser.v
33
parser.v
@@ -12,6 +12,7 @@ struct VarSymbolInfo {
|
||||
|
||||
struct FuncSymbolInfo {
|
||||
type string
|
||||
block Block
|
||||
}
|
||||
|
||||
struct SymbolTable {
|
||||
@@ -21,19 +22,21 @@ mut:
|
||||
}
|
||||
|
||||
fn (mut s SymbolTable) define_var(name string, typ string) {
|
||||
$if debug {
|
||||
dump(s.variable_scopes.len)
|
||||
}
|
||||
$if debug {
|
||||
dump(s.variable_scopes.len)
|
||||
}
|
||||
|
||||
if s.variable_scopes.len == 0 {
|
||||
parse_error('No scope available')
|
||||
}
|
||||
if s.variable_scopes.len == 0 {
|
||||
parse_error('No scope available')
|
||||
}
|
||||
|
||||
if name in s.variable_scopes[s.variable_scopes.len-1] {
|
||||
parse_error('Variable ${name} already defined in this scope')
|
||||
}
|
||||
if name in s.variable_scopes[s.variable_scopes.len-1] {
|
||||
parse_error('Variable ${name} already defined in this scope')
|
||||
}
|
||||
|
||||
s.variable_scopes[s.variable_scopes.len-1][name] = VarSymbolInfo{type: typ}
|
||||
s.variable_scopes[s.variable_scopes.len-1][name] = VarSymbolInfo{
|
||||
type: typ
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut s SymbolTable) lookup_var(name string) ?VarSymbolInfo {
|
||||
@@ -49,8 +52,8 @@ fn (mut s SymbolTable) lookup_var(name string) ?VarSymbolInfo {
|
||||
return none
|
||||
}
|
||||
|
||||
fn (mut s SymbolTable) define_func(name string, typ string) {
|
||||
s.functions[name] = FuncSymbolInfo{type: typ}
|
||||
fn (mut s SymbolTable) define_func(name string, typ string, block Block) {
|
||||
s.functions[name] = FuncSymbolInfo{type: typ, block: block}
|
||||
}
|
||||
|
||||
fn (mut s SymbolTable) lookup_func(name string) ?FuncSymbolInfo {
|
||||
@@ -68,6 +71,7 @@ fn (mut s SymbolTable) is_in_global_scope() bool {
|
||||
// ------------------------------------------- Expressions
|
||||
|
||||
type Expr = VoidExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function
|
||||
type LiteralExpr = IntegerLiteral | RealLiteral | BoolLiteral
|
||||
|
||||
struct VoidExpr {}
|
||||
|
||||
@@ -333,7 +337,7 @@ fn (mut p Parser) parse_func_decl() FuncDecl {
|
||||
|
||||
p.symbols.variable_scopes.delete_last()
|
||||
|
||||
p.symbols.define_func(name_tok.text, type_tok.text)
|
||||
p.symbols.define_func(name_tok.text, type_tok.text, block)
|
||||
|
||||
return FuncDecl {
|
||||
name: name_tok.text
|
||||
@@ -419,6 +423,9 @@ fn (mut p Parser) parse_program() []Stmt {
|
||||
p.statements << p.parse_statement()
|
||||
}
|
||||
p.symbols.variable_scopes.delete_last()
|
||||
$if debug {
|
||||
dump(p.symbols.functions)
|
||||
}
|
||||
return p.statements
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user