Add parser for structs, unions and functions

This commit is contained in:
Slendi 2024-02-09 14:34:02 +02:00
parent d5990adfee
commit 540e94037e
5 changed files with 466 additions and 142 deletions

View File

@ -10,22 +10,25 @@ NodeKind :: enum {
String,
Identifier,
//
Block,
BinaryExpression,
UnaryExpression,
FieldAccess,
IndexAccess,
FunctionCall,
VariableDeclaration,
//
Function,
Struct,
Enum,
Union,
Use,
//
If,
VariableDeclaration,
For,
}
Node :: struct {
@ -56,7 +59,14 @@ node_create_block :: proc(range: TextRange, children: [dynamic]^Node) -> (ret: ^
return
}
node_create_binary :: proc(kind: TokenKind, range: TextRange, left: ^Node, right: ^Node) -> (ret: ^Node) {
node_create_binary :: proc(
kind: TokenKind,
range: TextRange,
left: ^Node,
right: ^Node,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = .BinaryExpression,
@ -98,7 +108,13 @@ node_create_index_access :: proc(range: TextRange, left: ^Node, right: ^Node) ->
return
}
node_create_function_call :: proc(range: TextRange, name: ^Node, args: [dynamic]^Node) -> (ret: ^Node) {
node_create_function_call :: proc(
range: TextRange,
name: ^Node,
args: [dynamic]^Node,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = .FunctionCall,
@ -111,6 +127,27 @@ node_create_function_call :: proc(range: TextRange, name: ^Node, args: [dynamic]
return
}
node_create_function :: proc(
range: TextRange,
name: [dynamic]u8,
return_type, body: ^Node,
args: [dynamic]^Node,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = .Function,
range = range,
children = {return_type, body},
value = name,
}
for arg in args {
append(&ret.children, arg)
}
return
}
node_print :: proc(node: ^Node, indent := 0) {
for i in 0 ..< indent {
fmt.printf(" ")
@ -145,7 +182,11 @@ node_create_use :: proc(range: TextRange, path, alias: [dynamic]u8) -> (ret: ^No
path_ := path
// Check if the path ends with ".cat", if not, append it.
len_path := len(path_)
if len(path_) < 4 || path_[len_path - 4] != '.' || path_[len_path - 3] != 'c' || path_[len_path - 2] != 'a' || path_[len_path - 1] != 't' {
if len(path_) < 4 ||
path_[len_path - 4] != '.' ||
path_[len_path - 3] != 'c' ||
path_[len_path - 2] != 'a' ||
path_[len_path - 1] != 't' {
append(&path_, '.')
append(&path_, 'c')
append(&path_, 'a')
@ -197,7 +238,23 @@ node_create_if :: proc(range: TextRange, condition, then, else_: ^Node) -> (ret:
return
}
node_create_variable :: proc(range: TextRange, name, type_, value: ^Node, is_const: bool) -> (ret: ^Node) {
node_create_for :: proc(range: TextRange, init, condition, step, body: ^Node) -> (ret: ^Node) {
ret = new(Node)
ret^ = {
kind = .For,
range = range,
children = {init, condition, step, body},
}
return
}
node_create_variable :: proc(
range: TextRange,
name, type_, value: ^Node,
is_const: bool,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = .VariableDeclaration,
@ -208,3 +265,22 @@ node_create_variable :: proc(range: TextRange, name, type_, value: ^Node, is_con
return
}
node_create_struct_enum_or_union :: proc(
range: TextRange,
kind: NodeKind,
name: [dynamic]u8,
fields: [dynamic]^Node,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = kind,
range = range,
value = name,
}
for field in fields {
append(&ret.children, field)
}
return
}

View File

@ -8,7 +8,6 @@ Lexer :: struct {
data: ^[dynamic]u8,
read_position: u64,
position: TextPosition,
char, next: u8,
last_token_kind: TokenKind,
should_return_semicolon: bool,
@ -19,10 +18,7 @@ lexer_create :: proc(data: ^[dynamic]u8) -> ^Lexer {
lexer^ = {
data = data,
read_position = 0,
position = TextPosition {
line = 1,
column = 1,
},
position = TextPosition{line = 1, column = 1},
}
lexer_advance(lexer)
lexer_advance(lexer)
@ -51,13 +47,17 @@ lexer_advance :: proc(lexer: ^Lexer) {
@(private = "file")
lexer_should_not_emit_semicolon :: proc(lexer: ^Lexer) -> bool {
return lexer.last_token_kind == .CloseBrace ||
return(
lexer.last_token_kind == .CloseBrace ||
lexer.last_token_kind == .Semicolon ||
lexer.last_token_kind == .EOF ||
lexer.last_token_kind == .Invalid ||
lexer.last_token_kind == .OpenParen ||
lexer.last_token_kind == .OpenBrace ||
lexer.last_token_kind == .OpenBracket ||
lexer.last_token_kind == .CloseParen ||
lexer.last_token_kind == .CloseBrace ||
lexer.last_token_kind == .CloseBracket ||
lexer.last_token_kind == .Add ||
lexer.last_token_kind == .Subtract ||
lexer.last_token_kind == .Multiply ||
@ -73,7 +73,9 @@ lexer_should_not_emit_semicolon :: proc(lexer: ^Lexer) -> bool {
lexer.last_token_kind == .LessThan ||
lexer.last_token_kind == .GreaterThan ||
lexer.last_token_kind == .BitwiseLeftShift ||
lexer.last_token_kind == .BitwiseRightShift
lexer.last_token_kind == .BitwiseRightShift ||
lexer.last_token_kind == .Comma \
)
}
@(private = "file")
@ -135,10 +137,14 @@ lexer_next :: proc(lexer: ^Lexer) -> (ret: Token) {
crange.end = lexer.position
ret = token_create(.Arrow, crange)
}
case '*': ret = token_create(.Multiply, crange)
case '/': ret = token_create(.Divide, crange)
case '%': ret = token_create(.Modulo, crange)
case '`': ret = token_create(.Exponent, crange)
case '*':
ret = token_create(.Multiply, crange)
case '/':
ret = token_create(.Divide, crange)
case '%':
ret = token_create(.Modulo, crange)
case '`':
ret = token_create(.Exponent, crange)
case '=':
ret = token_create(.Assign, crange)
if lexer.next == '=' {
@ -175,27 +181,46 @@ lexer_next :: proc(lexer: ^Lexer) -> (ret: Token) {
crange.end = lexer.position
ret = token_create(.BitwiseRightShift, crange)
}
case '&': ret = token_create(.BitwiseAnd, crange)
case '|': ret = token_create(.BitwiseOr, crange)
case '^': ret = token_create(.BitwiseXOR, crange)
case '~': ret = token_create(.BitwiseNot, crange)
case '(': ret = token_create(.OpenParen, crange)
case ')': ret = token_create(.CloseParen, crange)
case '[': ret = token_create(.OpenBracket, crange)
case ']': ret = token_create(.CloseBracket, crange)
case '{': ret = token_create(.OpenBrace, crange)
case '}': ret = token_create(.CloseBrace, crange)
case '&':
ret = token_create(.BitwiseAnd, crange)
case '|':
ret = token_create(.BitwiseOr, crange)
case '^':
ret = token_create(.BitwiseXOR, crange)
case '~':
ret = token_create(.BitwiseNot, crange)
case '(':
ret = token_create(.OpenParen, crange)
case ')':
ret = token_create(.CloseParen, crange)
case '[':
ret = token_create(.OpenBracket, crange)
case ']':
ret = token_create(.CloseBracket, crange)
case '{':
ret = token_create(.OpenBrace, crange)
case '}':
ret = token_create(.CloseBrace, crange)
case '?': ret = token_create(.Question, crange)
case ':': ret = token_create(.Colon, crange)
case '.': ret = token_create(.Dot, crange)
case ',': ret = token_create(.Comma, crange)
case ';': ret = token_create(.Semicolon, crange)
case '?':
ret = token_create(.Question, crange)
case ':':
ret = token_create(.Colon, crange)
case '.':
ret = token_create(.Dot, crange)
case ',':
ret = token_create(.Comma, crange)
case ';':
ret = token_create(.Semicolon, crange)
case '"': ret = lexer_read_string(lexer, .String, '\"')
case '\'': ret = lexer_read_string(lexer, .Character, '\'')
case 'a'..='z': fallthrough
case 'A'..='Z': fallthrough
case '"':
ret = lexer_read_string(lexer, .String, '\"')
case '\'':
ret = lexer_read_string(lexer, .Character, '\'')
case 'a' ..= 'z':
fallthrough
case 'A' ..= 'Z':
fallthrough
case '_':
ret = lexer_read_identifier(lexer)
should_advance = false
@ -226,18 +251,34 @@ lexer_read_string :: proc(lexer: ^Lexer, kind: TokenKind, outer: u8) -> Token {
str: [dynamic]u8
for lexer.char != outer {
if lexer.char == '\\' {
range := TextRange { start = lexer.position }
range := TextRange {
start = lexer.position,
}
lexer_advance(lexer)
switch lexer.char {
case 'n': append(&str, '\n'); break
case 't': append(&str, '\t'); break
case 'b': append(&str, '\b'); break
case 'r': append(&str, '\r'); break
case '\\': append(&str, '\\'); break
case 'n':
append(&str, '\n');break
case 't':
append(&str, '\t');break
case 'b':
append(&str, '\b');break
case 'r':
append(&str, '\r');break
case '\\':
append(&str, '\\');break
case:
range.end = lexer.position
append(&g_message_list,
message_create(.Warning, fmt.aprintf("Invalid string/character escape: %c at %s", lexer.char, "TODO LOCATION"), range),
append(
&g_message_list,
message_create(
.Warning,
fmt.aprintf(
"Invalid string/character escape: %c at %s",
lexer.char,
"TODO LOCATION",
),
range,
),
)
}
lexer_advance(lexer)
@ -254,7 +295,9 @@ lexer_read_string :: proc(lexer: ^Lexer, kind: TokenKind, outer: u8) -> Token {
@(private = "file")
lexer_read_identifier :: proc(lexer: ^Lexer) -> Token {
crange := TextRange { start = lexer.position }
crange := TextRange {
start = lexer.position,
}
str: [dynamic]u8
for libc.isalnum(i32(lexer.char)) != 0 || lexer.char == '_' {
@ -263,31 +306,10 @@ lexer_read_identifier :: proc(lexer: ^Lexer) -> Token {
lexer_advance(lexer)
}
if compare_dyn_arr_string(&str, "fn") { return token_create(.Function, crange) }
else if compare_dyn_arr_string(&str, "struct") { return token_create(.Struct, crange) }
else if compare_dyn_arr_string(&str, "enum") { return token_create(.Enum, crange) }
else if compare_dyn_arr_string(&str, "union") { return token_create(.Union, crange) }
else if compare_dyn_arr_string(&str, "type") { return token_create(.Type, crange) }
else if compare_dyn_arr_string(&str, "use") { return token_create(.Use, crange) }
else if compare_dyn_arr_string(&str, "pub") { return token_create(.Pub, crange) }
else if compare_dyn_arr_string(&str, "let") { return token_create(.Let, crange) }
else if compare_dyn_arr_string(&str, "mut") { return token_create(.Mut, crange) }
else if compare_dyn_arr_string(&str, "as") { return token_create(.As, crange) }
else if compare_dyn_arr_string(&str, "in") { return token_create(.In, crange) }
else if compare_dyn_arr_string(&str, "if") { return token_create(.If, crange) }
else if compare_dyn_arr_string(&str, "else") { return token_create(.Else, crange) }
else if compare_dyn_arr_string(&str, "elif") { return token_create(.Elif, crange) }
else if compare_dyn_arr_string(&str, "for") { return token_create(.For, crange) }
else if compare_dyn_arr_string(&str, "break") { return token_create(.Break, crange) }
else if compare_dyn_arr_string(&str, "continue") { return token_create(.Continue, crange) }
else if compare_dyn_arr_string(&str, "switch") { return token_create(.Switch, crange) }
else if compare_dyn_arr_string(&str, "case") { return token_create(.Case, crange) }
else if compare_dyn_arr_string(&str, "ret") { return token_create(.Ret, crange) }
else if compare_dyn_arr_string(&str, "static") { return token_create(.Static, crange) }
else if compare_dyn_arr_string(&str, "defer") { return token_create(.Defer, crange) }
else if compare_dyn_arr_string(&str, "let") { return token_create(.Let, crange) }
else if compare_dyn_arr_string(&str, "and") { return token_create(.And, crange) }
else if compare_dyn_arr_string(&str, "or") { return token_create(.Or, crange) }
if compare_dyn_arr_string(
&str,
"fn",
) {return token_create(.Function, crange)} else if compare_dyn_arr_string(&str, "struct") {return token_create(.Struct, crange)} else if compare_dyn_arr_string(&str, "enum") {return token_create(.Enum, crange)} else if compare_dyn_arr_string(&str, "union") {return token_create(.Union, crange)} else if compare_dyn_arr_string(&str, "type") {return token_create(.Type, crange)} else if compare_dyn_arr_string(&str, "use") {return token_create(.Use, crange)} else if compare_dyn_arr_string(&str, "pub") {return token_create(.Pub, crange)} else if compare_dyn_arr_string(&str, "let") {return token_create(.Let, crange)} else if compare_dyn_arr_string(&str, "mut") {return token_create(.Mut, crange)} else if compare_dyn_arr_string(&str, "as") {return token_create(.As, crange)} else if compare_dyn_arr_string(&str, "in") {return token_create(.In, crange)} else if compare_dyn_arr_string(&str, "if") {return token_create(.If, crange)} else if compare_dyn_arr_string(&str, "else") {return token_create(.Else, crange)} else if compare_dyn_arr_string(&str, "elif") {return token_create(.Elif, crange)} else if compare_dyn_arr_string(&str, "for") {return token_create(.For, crange)} else if compare_dyn_arr_string(&str, "break") {return token_create(.Break, crange)} else if compare_dyn_arr_string(&str, "continue") {return token_create(.Continue, crange)} else if compare_dyn_arr_string(&str, "switch") {return token_create(.Switch, crange)} else if compare_dyn_arr_string(&str, "case") {return token_create(.Case, crange)} else if compare_dyn_arr_string(&str, "ret") {return token_create(.Ret, crange)} else if compare_dyn_arr_string(&str, "static") {return token_create(.Static, crange)} else if compare_dyn_arr_string(&str, "defer") {return token_create(.Defer, crange)} else if compare_dyn_arr_string(&str, "let") {return token_create(.Let, crange)} else if compare_dyn_arr_string(&str, "and") {return token_create(.And, crange)} else if compare_dyn_arr_string(&str, "or") {return token_create(.Or, crange)}
return token_create_u8(.Identifier, str, crange)
}
@ -346,7 +368,14 @@ lexer_read_number :: proc(lexer: ^Lexer) -> Token {
lexer_advance(lexer)
}
} else if read_mode == .Hex {
append(&g_message_list, message_create(.Error, "Hexadecimal floating point numbers are not supported yet", crange))
append(
&g_message_list,
message_create(
.Error,
"Hexadecimal floating point numbers are not supported yet",
crange,
),
)
lowered := libc.tolower(i32(lexer.char))
for libc.isxdigit(lowered) != 0 && lexer.char > 0 {
digit := lowered - '0'
@ -375,4 +404,3 @@ lexer_read_number :: proc(lexer: ^Lexer) -> Token {
return token_create_u64(.Integer, whole_part, crange)
}

11
ols.json Normal file
View File

@ -0,0 +1,11 @@
{
"collections": {
"name": "main",
"path": ".",
},
"enable_document_symbols": true,
"enable_semantic_tokens": true,
"enable_snippets": true,
"enable_references": true,
}

View File

@ -4,7 +4,6 @@ import "core:fmt"
Parser :: struct {
lexer: ^Lexer,
tok, next: Token,
can_be_function: bool,
}
@ -37,7 +36,14 @@ accept :: proc(parser: ^Parser, tok: TokenKind) -> bool {
@(private = "file")
expect :: proc(parser: ^Parser, tok: TokenKind) -> bool {
if !accept(parser, tok) {
append(&g_message_list, message_create(.Error, fmt.aprintf("Expected {}, got {} at {}", tok, parser.tok.kind, "TODO"), parser.tok.range))
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Expected {}, got {} at {}", tok, parser.tok.kind, "TODO"),
parser.tok.range,
),
)
return false
}
return true
@ -57,8 +63,10 @@ parser_parse_block :: proc(parser: ^Parser, end: TokenKind) -> (ret: ^Node) {
ret := parser_parse_definitions(parser)
expect(parser, .Semicolon)
for stmt in ret {
if stmt != nil {
append(&statements, stmt)
}
}
} else {
stmt := parser_parse_statement(parser)
if stmt != nil {
@ -78,16 +86,133 @@ parser_parse_statement :: proc(parser: ^Parser) -> (ret: ^Node) {
} else if parser.tok.kind == .If {
expect(parser, .If)
ret = parser_parse_if_statement(parser)
ret.range.start = range_beg.start
} else if parser.tok.kind == .Use {
ret = parser_parse_use_statement(parser)
expect(parser, .Semicolon)
} else if parser.tok.kind == .OpenBrace {
ret = parser_parse_block(parser, .CloseBrace)
} else if parser.tok.kind == .For {
ret = parser_parse_for_statement(parser)
} else if parser.tok.kind == .Function {
ret = parser_parse_function_definition(parser)
} else if parser.tok.kind == .Struct {
ret = parser_parse_struct_definition(parser)
fmt.printf("{} {}\n", parser.tok, parser.next)
} else if parser.tok.kind == .Enum {
ret = parser_parse_enum_definition(parser)
} else if parser.tok.kind == .Union {
ret = parser_parse_union_definition(parser)
} else {
ret = parser_parse_expression(parser)
expect(parser, .Semicolon)
}
if ret != nil {
ret.range.start = range_beg.start
}
return
}
@(private = "file")
parser_parse_struct_definition :: proc(parser: ^Parser) -> ^Node {
range := parser.tok.range
expect(parser, .Struct)
name: [dynamic]u8
if parser.tok.kind == .Identifier {
name = parser.tok.value.([dynamic]u8)
parser_next(parser)
} else {
expect(parser, .Identifier)
}
expect(parser, .OpenBrace)
fields := parser_parse_definitions(parser, .CloseBrace)
expect(parser, .CloseBrace)
return node_create_struct_enum_or_union(range, .Struct, name, fields)
}
@(private = "file")
parser_parse_enum_definition :: proc(parser: ^Parser) -> ^Node {
range := parser.tok.range
expect(parser, .Enum)
panic("TODO, enum not implemented yet")
}
@(private = "file")
parser_parse_union_definition :: proc(parser: ^Parser) -> ^Node {
range := parser.tok.range
expect(parser, .Union)
name: [dynamic]u8
if parser.tok.kind == .Identifier {
name = parser.tok.value.([dynamic]u8)
parser_next(parser)
} else {
expect(parser, .Identifier)
}
expect(parser, .OpenBrace)
fields := parser_parse_definitions(parser, .CloseBrace)
expect(parser, .CloseBrace)
return node_create_struct_enum_or_union(range, .Union, name, fields)
}
@(private = "file")
parser_parse_function_definition :: proc(parser: ^Parser) -> ^Node {
expect(parser, .Function)
name: [dynamic]u8
if parser.tok.kind == .Identifier {
name = parser.tok.value.([dynamic]u8)
parser_next(parser)
} else {
expect(parser, .Identifier)
}
params: [dynamic]^Node
if accept(parser, .OpenParen) {
params = parser_parse_definitions(parser, .CloseParen)
expect(parser, .CloseParen)
} else {
params = {}
}
type: ^Node = nil
if parser.tok.kind != .OpenBrace {
type = parser_parse_type(parser)
}
expect(parser, .OpenBrace)
body := parser_parse_block(parser, .CloseBrace)
return node_create_function(parser.tok.range, name, type, body, params)
}
@(private = "file")
parser_parse_for_statement :: proc(parser: ^Parser) -> ^Node {
range := parser.tok.range
expect(parser, .For)
if accept(parser, .OpenBrace) {
body := parser_parse_block(parser, .CloseBrace)
return node_create_for(range, nil, nil, nil, body)
}
if parser.tok.kind == .Let {
panic("TODO, let in for not implemented yet")
}
init := parser_parse_expression(parser)
if accept(parser, .OpenBrace) {
body := parser_parse_block(parser, .CloseBrace)
return node_create_for(range, nil, init, nil, body)
}
expect(parser, .Semicolon)
condition: ^Node = nil
if parser.tok.kind != .Semicolon {
condition = parser_parse_expression(parser)
}
expect(parser, .Semicolon)
if accept(parser, .OpenBrace) {
body := parser_parse_block(parser, .CloseBrace)
return node_create_for(range, init, condition, nil, body)
}
after := parser_parse_expression(parser)
expect(parser, .OpenBrace)
body := parser_parse_block(parser, .CloseBrace)
return node_create_for(range, init, condition, after, body)
}
@(private = "file")
parser_parse_type :: proc(parser: ^Parser) -> (ret: ^Node) {
// FIXME: Add more types
@ -96,10 +221,14 @@ parser_parse_type :: proc(parser: ^Parser) -> (ret: ^Node) {
ret = node_create_value(.Identifier, range, parser.tok.value.([dynamic]u8))
parser_next(parser)
} else {
append(&g_message_list, message_create(
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Expected type, got {} at {}", parser.tok.kind, "TODO"),
parser.tok.range))
parser.tok.range,
),
)
ret = nil
}
return
@ -197,7 +326,11 @@ parser_parse_expression :: proc(parser: ^Parser) -> ^Node {
}
@(private = "file")
parser_parse_binary_expression :: proc(parser: ^Parser, kinds: []TokenKind, next: proc(parser: ^Parser) -> ^Node) -> ^Node {
parser_parse_binary_expression :: proc(
parser: ^Parser,
kinds: []TokenKind,
next: proc(parser: ^Parser) -> ^Node,
) -> ^Node {
lhs := next(parser)
for kind in kinds {
for accept(parser, kind) {
@ -224,10 +357,14 @@ parser_parse_arrow :: proc(parser: ^Parser) -> ^Node {
rhs.kind != .Identifier &&
rhs.kind != .FieldAccess &&
rhs.kind != .IndexAccess {
append(&g_message_list, message_create(
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Expected function call, got {} at {}", rhs.kind, "TODO"),
rhs.range))
rhs.range,
),
)
return lhs
}
if rhs.kind != .FunctionCall {
@ -246,7 +383,11 @@ parser_parse_equality :: proc(parser: ^Parser) -> ^Node {
@(private = "file")
parser_parse_comparison :: proc(parser: ^Parser) -> ^Node {
return parser_parse_binary_expression(parser, {.LessThan, .LessThanOrEqual, .GreaterThan, .GreaterThanOrEqual}, parser_parse_addition)
return parser_parse_binary_expression(
parser,
{.LessThan, .LessThanOrEqual, .GreaterThan, .GreaterThanOrEqual},
parser_parse_addition,
)
}
@(private = "file")
@ -256,7 +397,11 @@ parser_parse_addition :: proc(parser: ^Parser) -> ^Node {
@(private = "file")
parser_parse_multiplication :: proc(parser: ^Parser) -> ^Node {
return parser_parse_binary_expression(parser, {.Multiply, .Divide, .Modulo}, parser_parse_exponent)
return parser_parse_binary_expression(
parser,
{.Multiply, .Divide, .Modulo},
parser_parse_exponent,
)
}
@(private = "file")
@ -266,7 +411,11 @@ parser_parse_exponent :: proc(parser: ^Parser) -> ^Node {
@(private = "file")
parser_parse_bitwise :: proc(parser: ^Parser) -> ^Node {
return parser_parse_binary_expression(parser, {.BitwiseAnd, .BitwiseOr, .BitwiseXOR, .BitwiseLeftShift, .BitwiseRightShift}, parser_parse_prefix_2)
return parser_parse_binary_expression(
parser,
{.BitwiseAnd, .BitwiseOr, .BitwiseXOR, .BitwiseLeftShift, .BitwiseRightShift},
parser_parse_prefix_2,
)
}
@(private = "file")
@ -360,14 +509,26 @@ parser_parse_factor :: proc(parser: ^Parser) -> (ret: ^Node) {
prev := parser.can_be_function
parser.can_be_function = false
if accept(parser, .Dot) {
ret = node_create_field_access({ ret.range.start, parser.tok.range.start }, ret, parser_parse_factor(parser))
ret = node_create_field_access(
{ret.range.start, parser.tok.range.start},
ret,
parser_parse_factor(parser),
)
}
parser.can_be_function = prev
if parser.can_be_function && parser.tok.kind != .CloseParen && parser.tok.kind != .Semicolon && parser.tok.kind != .Arrow && parser.tok.kind != .EOF {
if parser.can_be_function &&
parser.tok.kind != .CloseParen &&
parser.tok.kind != .Semicolon &&
parser.tok.kind != .Arrow &&
parser.tok.kind != .EOF {
prev := parser.can_be_function
parser.can_be_function = false
args: [dynamic]^Node
for parser.tok.kind != .CloseParen && parser.tok.kind != .Semicolon && parser.tok.kind != .Arrow && parser.tok.kind != .EOF && parser_is_factor_token_or_prefix(parser.tok.kind) {
for parser.tok.kind != .CloseParen &&
parser.tok.kind != .Semicolon &&
parser.tok.kind != .Arrow &&
parser.tok.kind != .EOF &&
parser_is_factor_token_or_prefix(parser.tok.kind) {
append(&args, parser_parse_expression(parser))
}
ret = node_create_function_call(ret.range, ret, args)
@ -380,8 +541,14 @@ parser_parse_factor :: proc(parser: ^Parser) -> (ret: ^Node) {
parser.can_be_function = prev
expect(parser, .CloseParen)
} else {
append(&g_message_list, message_create(.Error, fmt.aprintf("Unexpected factor token {} at {}", parser.tok.kind, "TODO"), parser.tok.range))
append(
&g_message_list,
message_create(
.Error,
fmt.aprintf("Unexpected factor token {} at {}", parser.tok.kind, "TODO"),
parser.tok.range,
),
)
}
return
}

View File

@ -4,7 +4,7 @@ use "directory/library'with'special'chars"
\ This is a comment, it should be ignored by the compiler
fmt.printf "%d + %d = %d File length: %d" a b a + b (io.file_size "file.txt")
fmt.printf "%i + %i = %i File length: %i\n" a b a + b (io.file_size "file.txt")
fmt.println "Hello world!"
let a := 123 \ This is another comment, that should be ignored by the compiler
@ -19,3 +19,45 @@ if a == 1 {
} else {
aaaaaaa
}
for {
\ Infinite loop
}
for a > 0 {
\ Countdown loop
fmt.printf "%i\n" a--
}
\for let i : i32 = 0; i < 20; i++ {
\ \ Loop that goes up to 19
\}
\
\for let i : i32 in 0..<20 {
\ \ Shorthand for above
\}
fn name {}
fn name returntype {}
fn name () {}
fn name(param1 param2 param3: i32, param4: u32) u32 { }
struct StructName {
field1 field2 field3: i32,
field4: u32,
}
union MyUnion {
data: StructName,
some_other_data: EnumName,
}
\enum EnumName {
\ Value1,
\ Value2,
\ Value3,
\ Value4,
\}
\EnumName.Value1