Skip to content

Commit

Permalink
basic parser (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 3, 2024
1 parent 1b170d9 commit e425e84
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 45 deletions.
9 changes: 8 additions & 1 deletion compiler/ast/Expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

module ast

pub type Expr = RuneLit | IntegerLit
pub type Expr = EmptyExpr | RuneLit | IntegerLit

pub struct EmptyExpr {
pub:
pos FilePos
}

pub const empty_expr = EmptyExpr{}

pub struct RuneLit {
pub:
Expand Down
3 changes: 2 additions & 1 deletion compiler/ast/File.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub:
content string
mod_name string
pub mut:
scope &Scope = unsafe { nil }
errors int
scope &Scope = unsafe { nil }
stmts []Stmt
mut:
lines ?[]string
}
Expand Down
14 changes: 9 additions & 5 deletions compiler/ast/Stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

module ast

pub type Stmt = FnStmt
pub type Stmt = EmptyStmt | FnStmt

pub type EmptyStmt = u8

pub const empty_stmt = EmptyStmt(0)

pub struct FnStmt {
pub:
Expand All @@ -18,8 +22,8 @@ pub:

pub struct FnArg {
pub:
name string
name_pos FilePos
type Type
default_value ?Expr
name string
name_pos FilePos
type Type
default_expr ?Expr
}
46 changes: 45 additions & 1 deletion compiler/ast/Symbol.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,55 @@

module ast

pub type Symbol = Function | Constant
pub type Symbol = Function | Constant | TypeSym

pub struct TypeSym {
pub:
name string
kind TypeKind
fields []Field
}

pub enum TypeKind {
unknown
alias

i8
i16
i32
i64
int

u8
u16
u32
u64
uint

f32
f64

bool
rune

array
slice
tuple
struct
trait
enum
}

pub struct Field {
pub:
name string
type Type
}

pub struct Function {
pub:
name string
args []FnArg
node FnStmt
}

Expand Down
17 changes: 14 additions & 3 deletions compiler/ast/Type.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@

module ast

pub type Type = SimpleType | PointerType
pub type Type = VoidType | NoneType | NeverType | UnresolvedType | SimpleType | PointerType

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

pub struct VoidType {}

pub struct NoneType {}

pub struct NeverType {}

pub struct SimpleType {
pub:
sym Symbol
pos FilePos
}

pub struct PointerType {
pub:
inner Type
Expand Down
146 changes: 145 additions & 1 deletion compiler/context/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module context

import ast
import compiler.ast

const stack = []&CContext{}

Expand Down Expand Up @@ -36,8 +36,152 @@ pub mut:
options Options
report Report

universe &ast.Scope = ast.Scope.new(unsafe { nil })

root_file &ast.File = unsafe { nil }
files []&ast.File

// Types:
void_type ast.Type
none_type ast.Type
never_type ast.Type

i8_type ast.Type
i16_type ast.Type
i32_type ast.Type
i64_type ast.Type
int_type ast.Type

u8_type ast.Type
u16_type ast.Type
u32_type ast.Type
u64_type ast.Type
uint_type ast.Type

f32_type ast.Type
f64_type ast.Type

bool_type ast.Type
rune_type ast.Type
}

pub fn (mut ctx CContext) setup() {
ctx.load_universe()
ctx.load_primitive_types()
}

pub fn (mut ctx CContext) load_universe() {
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'i8'
kind: .i8
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'i16'
kind: .i16
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'i32'
kind: .i32
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'i64'
kind: .i64
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'int'
kind: .int
}) or { ic_error(err.msg()) }

ctx.universe.add_local_symbol(ast.TypeSym{
name: 'u8'
kind: .u8
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'u16'
kind: .u16
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'u32'
kind: .u32
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'u64'
kind: .u64
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'uint'
kind: .uint
}) or { ic_error(err.msg()) }

ctx.universe.add_local_symbol(ast.TypeSym{
name: 'f32'
kind: .f32
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'f64'
kind: .f64
}) or { ic_error(err.msg()) }

ctx.universe.add_local_symbol(ast.TypeSym{
name: 'bool'
kind: .bool
}) or { ic_error(err.msg()) }
ctx.universe.add_local_symbol(ast.TypeSym{
name: 'rune'
kind: .rune
}) or { ic_error(err.msg()) }
}

pub fn (mut ctx CContext) load_primitive_types() {
ctx.void_type = ast.VoidType{}
ctx.never_type = ast.NeverType{}
ctx.none_type = ast.NoneType{}

ctx.i8_type = ast.SimpleType{
sym: ctx.universe.find('i8') or { ic_error(err.msg()) }
}
ctx.i16_type = ast.SimpleType{
sym: ctx.universe.find('i16') or { ic_error(err.msg()) }
}
ctx.i32_type = ast.SimpleType{
sym: ctx.universe.find('i32') or { ic_error(err.msg()) }
}
ctx.i64_type = ast.SimpleType{
sym: ctx.universe.find('i64') or { ic_error(err.msg()) }
}
ctx.int_type = ast.SimpleType{
sym: ctx.universe.find('int') or { ic_error(err.msg()) }
}

ctx.u8_type = ast.SimpleType{
sym: ctx.universe.find('u8') or { ic_error(err.msg()) }
}
ctx.u16_type = ast.SimpleType{
sym: ctx.universe.find('u16') or { ic_error(err.msg()) }
}
ctx.u32_type = ast.SimpleType{
sym: ctx.universe.find('u32') or { ic_error(err.msg()) }
}
ctx.u64_type = ast.SimpleType{
sym: ctx.universe.find('u64') or { ic_error(err.msg()) }
}
ctx.uint_type = ast.SimpleType{
sym: ctx.universe.find('uint') or { ic_error(err.msg()) }
}

ctx.f32_type = ast.SimpleType{
sym: ctx.universe.find('f32') or { ic_error(err.msg()) }
}
ctx.f64_type = ast.SimpleType{
sym: ctx.universe.find('f64') or { ic_error(err.msg()) }
}

ctx.bool_type = ast.SimpleType{
sym: ctx.universe.find('bool') or { ic_error(err.msg()) }
}
ctx.rune_type = ast.SimpleType{
sym: ctx.universe.find('rune') or { ic_error(err.msg()) }
}
}

pub fn (ctx &CContext) abort_if_errors() {
Expand Down
6 changes: 5 additions & 1 deletion compiler/context/report.v
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ fn report_source(pos ast.FilePos, prefix string, mut sb strings.Builder) {
continue
}

if idx == pos.end.line && jdx == pos.end.col {
break
}

mut caret := false
if pos.begin.line == idx && pos.end.line == idx {
if pos.begin.col <= jdx + 1 && jdx + 1 <= pos.end.col {
Expand Down Expand Up @@ -163,7 +167,7 @@ fn print_highlighted_message(msg string, mut sb strings.Builder) {
start = cur
} else {
cur++
sb.write_string(cyan(msg[start..cur]))
sb.write_string(bold(blue(msg[start..cur])))
start = cur
}
} else {
Expand Down
1 change: 1 addition & 0 deletions compiler/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn run(args []string) {
context.push(ctx)
defer { context.pop() }

ctx.setup()
ctx.options = context.parse_args(args)

mut p := parser.new(ctx)
Expand Down
31 changes: 31 additions & 0 deletions compiler/parser/expr.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 parser

import compiler.ast
import compiler.context

fn (mut p Parser) parse_expr() ast.Expr {
match p.tok.kind {
.number {
return p.parse_integer_lit()
}
.char {
return p.parse_rune_lit()
}
else {
context.error('unexpected token', p.tok.pos)
}
}
return ast.empty_expr
}

fn (mut p Parser) parse_integer_lit() ast.IntegerLit {
return ast.IntegerLit{}
}

fn (mut p Parser) parse_rune_lit() ast.RuneLit {
return ast.RuneLit{}
}
Loading

0 comments on commit e425e84

Please sign in to comment.