Add support for prefix increment/decrement ops

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-04-11 09:20:15 +03:00
parent f65507c5e1
commit 4782252f7e
6 changed files with 37 additions and 24 deletions

View File

@ -74,8 +74,7 @@ let alpha := 1.0
for WindowShouldClose == 0 { for WindowShouldClose == 0 {
if state == 0 { if state == 0 {
fc = fc + 1 if ++fc == 120 {
if fc == 120 {
state = 1 state = 1
fc = 0 fc = 0
} }
@ -94,9 +93,8 @@ for WindowShouldClose == 0 {
state = 3 state = 3
} }
} elif state == 3 { } elif state == 3 {
fc = fc + 1 if ++fc / 12 != 0 {
if fc / 12 != 0 { ++lc
lc = lc + 1
fc = 0 fc = 0
} }
@ -153,6 +151,7 @@ for WindowShouldClose == 0 {
} else { } else {
DrawTextWrap "[R] REPLAY" 340 200 20 BLACK DrawTextWrap "[R] REPLAY" 340 200 20 BLACK
} }
EndDrawing EndDrawing
} }
CloseWindow CloseWindow

View File

@ -108,11 +108,12 @@ node_create_binary :: proc(kind: TokenKind, range: SourceLocation, left: ^Node,
return return
} }
node_create_unary :: proc(kind: TokenKind, range: SourceLocation, operand: ^Node) -> (ret: ^Node) { node_create_unary :: proc(kind: TokenKind, range: SourceLocation, operand: ^Node, post := false) -> (ret: ^Node) {
ret = new(Node) ret = new(Node)
ret^ = { ret^ = {
kind = .UnaryExpression, kind = .UnaryExpression,
range = range, range = range,
value = post,
children = {operand}, children = {operand},
value_token_kind = kind, value_token_kind = kind,
} }
@ -218,7 +219,7 @@ node_print :: proc(node: ^Node, indent := 0) {
fmt.printf("{} ", node.value_token_kind) fmt.printf("{} ", node.value_token_kind)
} }
if node.return_type != nil { if node.return_type != nil {
fmt.printf("-> {} ", node.return_type) fmt.printf("-> {} ", type_to_string(node.return_type))
} }
fmt.println("") fmt.println("")
@ -399,3 +400,9 @@ node_create_struct_enum_or_union :: proc(
} }
return return
} }
node_clone :: proc(node: ^Node) -> (ret: ^Node) {
ret = new(Node)
ret^ = node^
return
}

View File

@ -65,6 +65,8 @@ main :: proc() {
} }
} }
node_print(ast)
name: string name: string
if handle == os.stdin { if handle == os.stdin {
name = "stdin" name = "stdin"

View File

@ -524,9 +524,9 @@ parser_parse_suffix :: proc(parser: ^Parser) -> ^Node {
expect(parser, .CloseBracket) expect(parser, .CloseBracket)
return node_create_index_access(range, lhs, rhs) return node_create_index_access(range, lhs, rhs)
} else if accept(parser, .Increment) { } else if accept(parser, .Increment) {
return node_create_unary(.Increment, range, lhs) return node_create_unary(.Increment, range, lhs, true)
} else if accept(parser, .Decrement) { } else if accept(parser, .Decrement) {
return node_create_unary(.Decrement, range, lhs) return node_create_unary(.Decrement, range, lhs, true)
} else if accept(parser, .As) { } else if accept(parser, .As) {
type := parser_parse_type(parser) type := parser_parse_type(parser)
range.range.end = type.range.range.end range.range.end = type.range.range.end
@ -564,9 +564,6 @@ parser_is_factor_token_or_prefix :: proc(tt: TokenKind) -> bool {
#partial switch tt { #partial switch tt {
case .Identifier, .Integer, .Float, .String, .Character, .OpenParen: case .Identifier, .Integer, .Float, .String, .Character, .OpenParen:
return true return true
case .Not, .Subtract, .BitwiseNot, .Increment, .Decrement:
// Assuming these can be used as prefixes in unary operations
return true
case: case:
return false return false
} }

View File

@ -458,15 +458,26 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
case .UnaryExpression: case .UnaryExpression:
// FIXME: Verify that the operation is possible // FIXME: Verify that the operation is possible
type_check(ast.children[0], ast) type_check(ast.children[0], ast)
append( append(&g_message_list, message_create(.FIXME, fmt.aprintf("Check type in unary expression"), ast.range))
&g_message_list,
message_create(
.FIXME,
fmt.aprintf("Check type before setting return type in unary expression"),
ast.range,
),
)
ast.return_type = ast.children[0].return_type ast.return_type = ast.children[0].return_type
if ast.value_token_kind == .Increment || ast.value_token_kind == .Decrement {
if ast.value.(bool) {
ast^ = ast.children[0]^
append(&g_message_list, message_create(.FIXME, fmt.aprintf("Implement postfix inc/dec"), ast.range))
} else {
ast.kind = .BinaryExpression
var := ast.children[0]
op: ^Node
if ast.value_token_kind == .Increment {
op = node_create_binary(.Add, ast.range, var, node_create_value(.Integer, ast.range, 1))
} else {
op = node_create_binary(.Subtract, ast.range, var, node_create_value(.Integer, ast.range, 1))
}
append(&ast.children, op)
type_check(ast.children[1], ast)
ast.value_token_kind = .Assign
}
}
case .Ret: case .Ret:
function_return_type := scope_function_return_type_lookup() function_return_type := scope_function_return_type_lookup()
if function_return_type == nil { if function_return_type == nil {

View File

@ -1,5 +1,2 @@
let inst := 123 a++
for {
inst = inst + 1
}