Skip to content

Commit

Permalink
astgen,grammar: fix type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jun 12, 2024
1 parent 2385ba6 commit f40208e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
41 changes: 16 additions & 25 deletions bsc/astgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,47 +488,38 @@ def access_modifier(self, *nodes):
return AccessModifier.private

# Types
def primitive_type(self, *nodes):
match nodes[0].value:
case "any":
return self.ctx.any_type
case "bool":
return self.ctx.bool_type
case "number":
return self.ctx.number_type
case "string":
return self.ctx.string_type
case _:
assert False # unreachable

def user_type(self, *names):
left = names[0]
for name in names[1:]:
if not isinstance(name, Ident):
continue
left = SelectorExpr(left, name, left.pos + name.pos)
if isinstance(left, Ident):
match left.name:
case "any":
return self.ctx.any_type
case "bool":
return self.ctx.bool_type
case "number":
return self.ctx.number_type
case "string":
return self.ctx.string_type
case _:
return BasicType(left, left.pos)
return BasicType(left, left.pos)

def option_type(self, *nodes):
return OptionType(nodes[1], self.mkpos(nodes[0]) + nodes[1].pos)
return OptionType(nodes[1], self.mkpos(nodes[0]))

def array_type(self, *nodes):
has_size = not isinstance(nodes[1], Token)
size = nodes[1] if has_size else None
return ArrayType(
size, nodes[3 if has_size else 2],
self.mkpos(nodes[0]) + nodes[-1].pos
size, nodes[3 if has_size else 2], self.mkpos(nodes[0])
)

def map_type(self, *nodes):
return MapType(
nodes[1], nodes[3],
self.mkpos(nodes[0]) + self.mkpos(nodes[-1])
)
return MapType(nodes[1], nodes[3], self.mkpos(nodes[0]))

def sum_type(self, *nodes):
types = list(filter(lambda node: not isinstance(node, Token), nodes))
return SumType(types, nodes[0].pos + nodes[-1].pos)
return SumType(types, nodes[0].pos)

# Utilities
def get_access_modifier(self, node):
Expand Down
9 changes: 1 addition & 8 deletions bsc/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,13 @@ fn_arg: NAME COLON type [OP_ASSIGN expr]

access_modifier: KW_PUB [LPAREN KW_PKG RPAREN] | KW_PROT

?type: primitive_type
| NAME (DOT NAME)* -> user_type
?type: path_expr -> user_type
| QUESTION type -> option_type
| LBRACKET expr? RBRACKET type -> array_type
| LBRACE type COLON type RBRACE -> map_type
| type PIPE type (PIPE type)* -> sum_type
| LPAREN type COMMA type (COMMA type)* RPAREN -> tuple_type

// TODO: do we implement thread as a generic type? `thread[T]` or "thread"
primitive_type: "any"
| "bool"
| "number"
| "string"

// Statements

?stmt: expr
Expand Down
20 changes: 14 additions & 6 deletions tests/syntax.bs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum WorldLevel {

fn from_string(s: string) Self {
unsafe {
"unsafe!"
@lua("function x() end")
}
return match s {
"overworld" => .overworld,
Expand All @@ -61,18 +61,20 @@ class Person {
"Second Key": 100
}

tuple: (number, ?lua.string) = (1, nil)
tuple: (number, ?string) = (1, nil)

fn setAge(self, age: number) {
self.age = age
}

fn mkdir(path: lua.string = ".") number {
fn mkdir(path: lua::string = ".") number {
@assert(path)
}
}

fn init() {
const age = 9

var w = WorldLevel.midgard
w = .undefined

Expand All @@ -84,9 +86,11 @@ fn init() {
println("Hello World!")
_ = math.pow(2)
std.my_func()

if true {
q = 3
}

if false {
s = if true {
99
Expand All @@ -96,16 +100,20 @@ fn init() {
100
}
}

while nil {
m += 4
}

match name {
"holaa" => m = 99,
"wb", "js" => m = 1000,
else => exit
else => exit(0)
}

var x = true
match {
true => true,
false => false
x => true,
else => false
}
}

0 comments on commit f40208e

Please sign in to comment.