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