Skip to content

Commit

Permalink
woops
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 3, 2024
1 parent fef9e11 commit 1b170d9
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
20 changes: 20 additions & 0 deletions compiler/ast/Expr.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

pub type Expr = RuneLit | IntegerLit

pub struct RuneLit {
pub:
value string
is_byte bool
pos FilePos
}

pub struct IntegerLit {
pub:
value string
pos FilePos
}
62 changes: 62 additions & 0 deletions compiler/ast/Scope.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

@[heap]
pub struct Scope {
pub mut:
owner Symbol
mut:
parent &Scope
syms []Symbol
}

@[inline]
pub fn Scope.new(parent &Scope) &Scope {
return &Scope{
parent: parent
}
}

@[inline]
pub fn (sc &Scope) derive() &Scope {
return &Scope{
owner: sc.owner
parent: sc
}
}

pub fn (mut sc Scope) add_symbol(sym Symbol) ! {
if _ := sc.lookup(sym.name) {
return error('duplicate variable `${sym.name}`')
}
sc.syms << sym
}

pub fn (mut sc Scope) add_local_symbol(sym Symbol) ! {
if _ := sc.find(sym.name) {
return error('duplicate symbol `${sym.name}`')
}
sc.syms << sym
}

pub fn (sc &Scope) find(name string) ?Symbol {
for sym in sc.syms {
if sym.name == name {
return sym
}
}
return none
}

pub fn (sc &Scope) lookup(name string) ?Symbol {
if sym := sc.find(name) {
return sym
}
if sc.owner is Function || sc.parent == unsafe { nil } {
return none
}
return sc.parent.lookup(name)
}
25 changes: 25 additions & 0 deletions compiler/ast/Stmt.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

pub type Stmt = FnStmt

pub struct FnStmt {
pub:
is_pub bool
name string
name_pos FilePos
args []FnArg
return_type Type
stmts []Stmt
}

pub struct FnArg {
pub:
name string
name_pos FilePos
type Type
default_value ?Expr
}
24 changes: 24 additions & 0 deletions compiler/ast/Symbol.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

pub type Symbol = Function | Constant

pub struct Function {
pub:
name string
node FnStmt
}

pub struct Constant {
pub:
name string
}

pub struct Variable {
pub:
name string
is_local bool
}
20 changes: 20 additions & 0 deletions compiler/ast/Type.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

pub type Type = SimpleType | PointerType

pub struct SimpleType {
pub:
expr Expr
sym ?Symbol
pos FilePos
}

pub struct PointerType {
pub:
inner Type
pos FilePos
}

0 comments on commit 1b170d9

Please sign in to comment.