Skip to content

Commit

Permalink
sema - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 26, 2024
1 parent cea48cb commit 3da65e7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
1 change: 0 additions & 1 deletion compiler/ast/File.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mut:
errors int
scope &Scope = unsafe { nil }
stmts []Stmt
pos FilePos
mut:
lines ?[]string
}
Expand Down
1 change: 1 addition & 0 deletions compiler/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ pub fn run(args []string) {
parser: p
}
s.analyze(ctx)
ctx.abort_if_errors()
}
}
10 changes: 6 additions & 4 deletions compiler/parser/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ pub fn new(ctx &context.CContext) &Parser {
}
}

@[inline]
pub fn (mut p Parser) parse() {
p.parse_file(p.ctx.options.input, true)
_ = p.parse_file(p.ctx.options.input, true)
}

fn (mut p Parser) parse_file(filename string, is_root bool) {
fn (mut p Parser) parse_file(filename string, is_root bool) ?&ast.File {
p.file = ast.File.new(filename)
if is_root {
p.ctx.root_file = p.file
Expand All @@ -47,17 +48,18 @@ fn (mut p Parser) parse_file(filename string, is_root bool) {
p.tokenizer = tokenizer.from_file(p.ctx, p.file)
if p.file.errors > 0 {
// if the tokenizer found errors in the file, let's skip it
return
return none
}

p.advance(2)
p.file.pos = p.tok.pos
for {
p.file.stmts << p.parse_stmt()
if p.should_abort() {
break
}
}

return p.file
}

fn (mut p Parser) next() {
Expand Down
4 changes: 4 additions & 0 deletions compiler/parser/stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ 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
}

mut old_expect_semicolon := p.expect_semicolon
mut old_tags := p.tags
defer {
Expand Down
46 changes: 41 additions & 5 deletions compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ pub struct Sema {
pub:
// Because other files can be imported using the builtin
// function `import`, we need to have access to the parser
// to generate the corresponding AST of each imported file
// to generate the corresponding AST of each imported file.
parser &parser.Parser
mut:
ctx &context.CContext = unsafe { nil }

file &ast.File = unsafe { nil }
sym ast.Symbol
scope &ast.Scope = unsafe { nil }
file &ast.File = unsafe { nil }
sym ast.Symbol
scope &ast.Scope = unsafe { nil }
first_pass bool
}

pub fn (mut sema Sema) analyze(ctx &context.CContext) {
Expand All @@ -29,13 +30,48 @@ 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_symbol(sema.sym) or { context.error(err.msg(), sema.file.pos) }
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')
}

sema.file.scope = ast.Scope.new(sema.ctx.universe, sema.sym)
sema.scope = sema.file.scope

sema.first_pass = true
sema.stmts(mut sema.file.stmts)

sema.first_pass = false
sema.stmts(mut sema.file.stmts)

sema.ctx.files << sema.file
}

fn (mut sema Sema) stmts(mut stmts []ast.Stmt) {
for mut stmt in stmts {
sema.stmt(mut stmt)
}
}

fn (mut sema Sema) stmt(mut stmt ast.Stmt) {
match mut stmt {
ast.FnStmt {
sema.fn_stmt(mut stmt)
}
else {}
}
}

fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) {
if sema.first_pass {
sema.scope.add_local_symbol(ast.Function{
name: stmt.name
args: stmt.args
node: unsafe { stmt }
}) or { context.error(err.msg(), stmt.name_pos) }
}
}

0 comments on commit 3da65e7

Please sign in to comment.