Skip to content

Commit

Permalink
sema - stmts - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 30, 2024
1 parent 40a2b80 commit e81cc56
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
34 changes: 21 additions & 13 deletions compiler/ast/Stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@ module ast

pub type Stmt = EmptyStmt | FnStmt | ExprStmt | LetStmt | WhileStmt | DeferStmt

pub type EmptyStmt = u8
pub struct EmptyStmt {
pub:
pos FilePos
}

pub const empty_stmt = Stmt(EmptyStmt(0))
@[inline]
pub fn empty_stmt(pos FilePos) Stmt {
return EmptyStmt{pos}
}

pub struct ExprStmt {
pub:
tags Tags
expr Expr
}

pub struct LetStmt {
pub:
tags Tags
lefts []Variable
right Expr
is_pub bool
}

pub struct FnStmt {
pub:
tags Tags
Expand All @@ -48,13 +46,22 @@ pub:

pub struct WhileStmt {
pub:
tags Tags
tags Tags
pub mut:
init_stmt ?LetStmt
cond Expr
continue_expr ?Expr
stmts []Stmt
}

pub struct LetStmt {
pub:
tags Tags
lefts []Variable
right Expr
is_pub bool
}

pub enum DeferMode {
default
success
Expand All @@ -63,7 +70,8 @@ pub enum DeferMode {

pub struct DeferStmt {
pub:
tags Tags
mode DeferMode
tags Tags
mode DeferMode
pub mut:
stmts []Stmt
}
4 changes: 2 additions & 2 deletions compiler/parser/stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn (mut p Parser) parse_simple_block() ([]ast.Stmt, ?ast.Expr) {

fn (mut p Parser) parse_stmt() ast.Stmt {
if p.should_abort() {
return ast.empty_stmt
return ast.empty_stmt(p.tok.pos)
}

mut old_expect_semicolon := p.expect_semicolon
Expand All @@ -86,7 +86,7 @@ fn (mut p Parser) parse_stmt() ast.Stmt {

// module stmts: fns, consts, vars, etc.
is_pub := !p.inside_local_scope && p.accept(.kw_pub)
mut stmt := ast.empty_stmt
mut stmt := ast.empty_stmt(p.tok.pos)
match p.tok.kind {
.kw_fn {
stmt = p.parse_fn_stmt(is_pub)
Expand Down
42 changes: 40 additions & 2 deletions compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub fn (mut sema Sema) analyze(ctx &context.CContext) {

fn (mut sema Sema) check_file(mut file ast.File) {
sema.file = file

sema.sym = ast.TypeSym{
name: sema.file.mod_name
kind: .struct
}

sema.ctx.universe.add_local_symbol(sema.sym) or {
context.ic_error('cannot load module `${file.mod_name}`, there is another symbol with the same name')
}
Expand Down Expand Up @@ -71,7 +71,21 @@ fn (mut sema Sema) stmt(mut stmt ast.Stmt) {
ast.FnStmt {
sema.fn_stmt(mut stmt)
}
else {}
ast.ExprStmt {
sema.expr_stmt(mut stmt)
}
ast.WhileStmt {
sema.while_stmt(mut stmt)
}
ast.LetStmt {
sema.let_stmt(mut stmt)
}
ast.DeferStmt {
sema.defer_stmt(mut stmt)
}
ast.EmptyStmt {
context.error('empty statement detected', stmt.pos)
}
}
}

Expand Down Expand Up @@ -107,3 +121,27 @@ fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) {
sema.scope = stmt.scope
sema.stmts(mut stmt.stmts)
}

fn (mut sema Sema) expr_stmt(mut stmt ast.ExprStmt) {
sema.expr(stmt.expr)
}

fn (mut sema Sema) while_stmt(mut stmt ast.WhileStmt) {
if stmt.init_stmt != none {
sema.let_stmt(mut stmt.init_stmt)
}
sema.expr(stmt.cond)
if stmt.continue_expr != none {
sema.expr(stmt.continue_expr)
}
sema.stmts(mut stmt.stmts)
}

fn (mut sema Sema) let_stmt(mut stmt ast.LetStmt) {
}

fn (mut sema Sema) defer_stmt(mut stmt ast.DeferStmt) {
sema.stmts(mut stmt.stmts)
}

fn (mut sema Sema) expr(expr ast.Expr) {}

0 comments on commit e81cc56

Please sign in to comment.