function calls
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
main
|
main
|
||||||
onev
|
onev
|
||||||
|
exec
|
||||||
*.exe
|
*.exe
|
||||||
*.exe~
|
*.exe~
|
||||||
*.so
|
*.so
|
||||||
|
|||||||
15
generator.v
15
generator.v
@@ -17,7 +17,7 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
|
|||||||
match stmt {
|
match stmt {
|
||||||
VarDecl {
|
VarDecl {
|
||||||
c_type := g.get_c_type(stmt.type)
|
c_type := g.get_c_type(stmt.type)
|
||||||
g.out.write_string('${c_type} ${stmt.name} = ')
|
g.out.write_string('${c_type} v${stmt.name}${stmt.scope_depth.str()} = ')
|
||||||
g.gen_expr(stmt.value)
|
g.gen_expr(stmt.value)
|
||||||
g.out.writeln(';')
|
g.out.writeln(';')
|
||||||
}
|
}
|
||||||
@@ -57,10 +57,9 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
|||||||
g.out.write_string(expr.val.str())
|
g.out.write_string(expr.val.str())
|
||||||
}
|
}
|
||||||
Variable {
|
Variable {
|
||||||
g.out.write_string(expr.name)
|
g.out.write_string('v${expr.name}${expr.scope_depth.str()}')
|
||||||
}
|
}
|
||||||
UnaryExpr {
|
UnaryExpr {
|
||||||
// Handle postfix/prefix logic if necessary
|
|
||||||
g.out.write_string('${expr.ident}${expr.op}')
|
g.out.write_string('${expr.ident}${expr.op}')
|
||||||
}
|
}
|
||||||
BinaryExpr {
|
BinaryExpr {
|
||||||
@@ -86,7 +85,15 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
|||||||
g.out.write_string('(${c_type})')
|
g.out.write_string('(${c_type})')
|
||||||
g.gen_expr(expr.expr)
|
g.gen_expr(expr.expr)
|
||||||
}
|
}
|
||||||
else {panic('unimplemented ${expr}')}
|
ParenExpr {
|
||||||
|
g.out.write_string('(')
|
||||||
|
g.gen_expr(expr.expr)
|
||||||
|
g.out.write_string(')')
|
||||||
|
}
|
||||||
|
FnCall {
|
||||||
|
g.out.write_string('${expr.name}()')
|
||||||
|
}
|
||||||
|
else {panic("Unimplemented expression")}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
main.v
2
main.v
@@ -31,6 +31,6 @@ fn main() {
|
|||||||
|
|
||||||
out_c := generator.gen_c(statements)
|
out_c := generator.gen_c(statements)
|
||||||
|
|
||||||
compile_with_clang(out_c, 'test', true)
|
compile_with_clang(out_c, 'exec', true)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
27
parser.v
27
parser.v
@@ -91,7 +91,7 @@ fn (mut s SymbolTable) is_in_global_scope() bool {
|
|||||||
|
|
||||||
// ------------------------------------------- Expressions
|
// ------------------------------------------- Expressions
|
||||||
|
|
||||||
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr
|
type Expr = VoidExpr | UnaryExpr | BinaryExpr | IntegerLiteral | RealLiteral | BoolLiteral | Variable | TypeExpr | Function | TypeCast | ParenExpr | PrintExpr | FnCall
|
||||||
|
|
||||||
struct VoidExpr {}
|
struct VoidExpr {}
|
||||||
|
|
||||||
@@ -120,6 +120,7 @@ struct BoolLiteral {
|
|||||||
|
|
||||||
struct Variable {
|
struct Variable {
|
||||||
name string
|
name string
|
||||||
|
scope_depth int
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TypeExpr {
|
struct TypeExpr {
|
||||||
@@ -144,6 +145,10 @@ struct PrintExpr {
|
|||||||
type string
|
type string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FnCall {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------- Statements
|
// ------------------------------------------- Statements
|
||||||
|
|
||||||
type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | FuncDecl
|
type Stmt = VarDecl | ExprStmt | ReturnStmt | Block | FuncDecl
|
||||||
@@ -152,6 +157,7 @@ struct VarDecl {
|
|||||||
name string
|
name string
|
||||||
type string
|
type string
|
||||||
value Expr
|
value Expr
|
||||||
|
scope_depth int
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FuncDecl {
|
struct FuncDecl {
|
||||||
@@ -253,10 +259,16 @@ fn (mut p Parser) parse_expr(prec Precedence) Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p Parser) parse_ident(ident string) Expr {
|
fn (mut p Parser) parse_ident(ident string) Expr {
|
||||||
return match p.peek().type {
|
expr := match p.peek().type {
|
||||||
.increment, .decrement {UnaryExpr {ident: ident, op: p.next().text}}
|
.increment, .decrement {Expr(UnaryExpr {ident: ident, op: p.next().text})}
|
||||||
else {Variable{ident}}
|
.lparen {
|
||||||
|
p.expect(.lparen)
|
||||||
|
p.expect(.rparen)
|
||||||
|
FnCall{name: ident}
|
||||||
}
|
}
|
||||||
|
else {Variable{ident, p.symbols.variable_scopes.len}}
|
||||||
|
}
|
||||||
|
return expr
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p Parser) parse_print() PrintExpr {
|
fn (mut p Parser) parse_print() PrintExpr {
|
||||||
@@ -333,6 +345,10 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
|
|||||||
return info.type
|
return info.type
|
||||||
}
|
}
|
||||||
TypeCast {expr.type}
|
TypeCast {expr.type}
|
||||||
|
FnCall {
|
||||||
|
fninfo := p.symbols.lookup_func(expr.name) or {parse_error("Tried to call undefined function ${expr.name}")}
|
||||||
|
fninfo.type
|
||||||
|
}
|
||||||
else {"Tried getting type of unexpected Expr"}
|
else {"Tried getting type of unexpected Expr"}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -407,6 +423,7 @@ fn (mut p Parser) parse_var_decl() VarDecl {
|
|||||||
name: name_tok.text
|
name: name_tok.text
|
||||||
value: val
|
value: val
|
||||||
type: type_tok.text
|
type: type_tok.text
|
||||||
|
scope_depth: p.symbols.variable_scopes.len
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,7 +480,7 @@ fn (mut p Parser) parse_return_stmt() ReturnStmt {
|
|||||||
.integer {IntegerLiteral{token.text.int()}}
|
.integer {IntegerLiteral{token.text.int()}}
|
||||||
.real {RealLiteral{token.text.f32()}}
|
.real {RealLiteral{token.text.f32()}}
|
||||||
.boolean {BoolLiteral{token.text == 'true'}}
|
.boolean {BoolLiteral{token.text == 'true'}}
|
||||||
.identifier {Variable{token.text}}
|
.identifier {Variable{token.text, p.symbols.variable_scopes.len}}
|
||||||
.semicolon {VoidExpr{}}
|
.semicolon {VoidExpr{}}
|
||||||
else {parse_error("Unexpected Token")}
|
else {parse_error("Unexpected Token")}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user