diff --git a/compiler/ast/Expr.v b/compiler/ast/Expr.v new file mode 100644 index 00000000..82cd5399 --- /dev/null +++ b/compiler/ast/Expr.v @@ -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 +} diff --git a/compiler/ast/Scope.v b/compiler/ast/Scope.v new file mode 100644 index 00000000..b549e991 --- /dev/null +++ b/compiler/ast/Scope.v @@ -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) +} diff --git a/compiler/ast/Stmt.v b/compiler/ast/Stmt.v new file mode 100644 index 00000000..d5ff5929 --- /dev/null +++ b/compiler/ast/Stmt.v @@ -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 +} diff --git a/compiler/ast/Symbol.v b/compiler/ast/Symbol.v new file mode 100644 index 00000000..abbb6cb9 --- /dev/null +++ b/compiler/ast/Symbol.v @@ -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 +} diff --git a/compiler/ast/Type.v b/compiler/ast/Type.v new file mode 100644 index 00000000..477d9658 --- /dev/null +++ b/compiler/ast/Type.v @@ -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 +}