This commit is contained in:
uan
2026-02-04 22:35:26 +01:00
parent 6c9679ff57
commit 3eaeefcd4c
3 changed files with 19 additions and 14 deletions

View File

@@ -8,8 +8,8 @@ mut:
out strings.Builder
}
fn mangle_var(name string, scope_depth int) string {
return 'v${name}${scope_depth.str()}'
fn mangle_var(name string) string {
return 'v${name}'
}
fn (mut g Generator) get_c_type(typ string) string {
@@ -21,7 +21,10 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
match stmt {
VarDecl {
c_type := g.get_c_type(stmt.type)
g.out.write_string('${c_type} ${mangle_var(stmt.name, stmt.scope_depth)} = ')
if stmt.const {
g.out.write_string('const ')
}
g.out.write_string('${c_type} ${mangle_var(stmt.name)} = ')
g.gen_expr(stmt.value)
g.out.writeln(';')
}
@@ -55,7 +58,7 @@ fn (mut g Generator) gen_stmt(stmt Stmt) {
}
Param {
c_type := g.get_c_type(stmt.type)
g.out.write_string('${c_type} ${mangle_var(stmt.name, 2)}')
g.out.write_string('${c_type} ${mangle_var(stmt.name)}')
}
}
}
@@ -72,7 +75,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
g.out.write_string(expr.val.str())
}
Variable {
g.out.write_string(mangle_var(expr.name, expr.scope_depth))
g.out.write_string(mangle_var(expr.name))
}
UnaryExpr {
g.out.write_string('${expr.ident}${expr.op}')
@@ -121,7 +124,7 @@ fn (mut g Generator) gen_expr(expr Expr) {
fn (mut g Generator) gen_c(program []Stmt) string {
g.out.writeln('#include <stdio.h>')
g.out.writeln('#include <stdbool.h>') // For your 'bool' types [cite: 4]
g.out.writeln('#include <stdbool.h>')
for stmt in program {
g.gen_stmt(stmt)
}

View File

@@ -4,6 +4,7 @@ import term
enum TokenType as u8 {
kw_let
kw_const
type
kw_if
kw_else
@@ -92,6 +93,7 @@ fn toktype_from_delimiter(delimiter string) TokenType {
fn toktype_from_kw(kw string) TokenType {
return match kw {
'let' {.kw_let}
'const' {.kw_const}
'void', 'real', 'bool', 'int' {.type}
'if' {.kw_if}
'else' {.kw_else}
@@ -116,7 +118,7 @@ fn is_real(str string) bool {
fn is_keyword(str string) bool {
return [
"void", "int", "real", "bool", "if", "else", "for", "break", "fn", "return", "let", "true", "false", "print"
"void", "int", "real", "bool", "if", "else", "for", "break", "fn", "return", "let", "const", "true", "false", "print"
].contains(str)
}

View File

@@ -120,7 +120,6 @@ struct BoolLiteral {
struct Variable {
name string
scope_depth int
}
struct TypeExpr {
@@ -158,7 +157,7 @@ struct VarDecl {
name string
type string
value Expr
scope_depth int
const bool
}
struct FuncDecl {
@@ -270,7 +269,7 @@ fn (mut p Parser) parse_ident(ident string) Expr {
return match p.peek().type {
.increment, .decrement {UnaryExpr {ident: ident, op: p.next().text}}
.lparen {p.parse_call(ident)}
else {Variable{ident, p.symbols.variable_scopes.len}}
else {Variable{ident}}
}
}
@@ -404,7 +403,8 @@ fn (mut p Parser) get_return_stmts_recursive(block Block) []ReturnStmt {
fn (mut p Parser) parse_statement() Stmt {
match p.peek().type {
.kw_let {return p.parse_var_decl()}
.kw_let {return p.parse_var_decl(false)}
.kw_const {return p.parse_var_decl(true)}
.kw_return {return p.parse_return_stmt()}
.kw_fn {return p.parse_func_decl()}
.lbracket {return p.parse_block(false)}
@@ -414,8 +414,8 @@ fn (mut p Parser) parse_statement() Stmt {
fn (mut p Parser) parse_var_decl() VarDecl {
p.expect(.kw_let)
fn (mut p Parser) parse_var_decl(is_const bool) VarDecl {
p.next()
name_tok := p.next()
if name_tok.type != .identifier {
@@ -444,7 +444,7 @@ fn (mut p Parser) parse_var_decl() VarDecl {
name: name_tok.text
value: val
type: type_tok.text
scope_depth: p.symbols.variable_scopes.len
const: is_const
}
}