-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |