SymbolTable stores function blocks

This commit is contained in:
uan
2026-02-04 09:06:21 +01:00
parent 52c92386d7
commit 7eb9e021dd

View File

@@ -12,6 +12,7 @@ struct VarSymbolInfo {
struct FuncSymbolInfo {
type string
block Block
}
struct SymbolTable {
@@ -33,7 +34,9 @@ fn (mut s SymbolTable) define_var(name string, typ string) {
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
}