Add support for prefix increment/decrement ops
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
parent
f65507c5e1
commit
4782252f7e
9
pong.cat
9
pong.cat
@ -74,8 +74,7 @@ let alpha := 1.0
|
||||
|
||||
for WindowShouldClose == 0 {
|
||||
if state == 0 {
|
||||
fc = fc + 1
|
||||
if fc == 120 {
|
||||
if ++fc == 120 {
|
||||
state = 1
|
||||
fc = 0
|
||||
}
|
||||
@ -94,9 +93,8 @@ for WindowShouldClose == 0 {
|
||||
state = 3
|
||||
}
|
||||
} elif state == 3 {
|
||||
fc = fc + 1
|
||||
if fc / 12 != 0 {
|
||||
lc = lc + 1
|
||||
if ++fc / 12 != 0 {
|
||||
++lc
|
||||
fc = 0
|
||||
}
|
||||
|
||||
@ -153,6 +151,7 @@ for WindowShouldClose == 0 {
|
||||
} else {
|
||||
DrawTextWrap "[R] REPLAY" 340 200 20 BLACK
|
||||
}
|
||||
|
||||
EndDrawing
|
||||
}
|
||||
CloseWindow
|
||||
|
11
src/ast.odin
11
src/ast.odin
@ -108,11 +108,12 @@ node_create_binary :: proc(kind: TokenKind, range: SourceLocation, left: ^Node,
|
||||
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^ = {
|
||||
kind = .UnaryExpression,
|
||||
range = range,
|
||||
value = post,
|
||||
children = {operand},
|
||||
value_token_kind = kind,
|
||||
}
|
||||
@ -218,7 +219,7 @@ node_print :: proc(node: ^Node, indent := 0) {
|
||||
fmt.printf("{} ", node.value_token_kind)
|
||||
}
|
||||
if node.return_type != nil {
|
||||
fmt.printf("-> {} ", node.return_type)
|
||||
fmt.printf("-> {} ", type_to_string(node.return_type))
|
||||
}
|
||||
fmt.println("")
|
||||
|
||||
@ -399,3 +400,9 @@ node_create_struct_enum_or_union :: proc(
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
node_clone :: proc(node: ^Node) -> (ret: ^Node) {
|
||||
ret = new(Node)
|
||||
ret^ = node^
|
||||
return
|
||||
}
|
||||
|
@ -65,6 +65,8 @@ main :: proc() {
|
||||
}
|
||||
}
|
||||
|
||||
node_print(ast)
|
||||
|
||||
name: string
|
||||
if handle == os.stdin {
|
||||
name = "stdin"
|
||||
|
@ -524,9 +524,9 @@ parser_parse_suffix :: proc(parser: ^Parser) -> ^Node {
|
||||
expect(parser, .CloseBracket)
|
||||
return node_create_index_access(range, lhs, rhs)
|
||||
} 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) {
|
||||
return node_create_unary(.Decrement, range, lhs)
|
||||
return node_create_unary(.Decrement, range, lhs, true)
|
||||
} else if accept(parser, .As) {
|
||||
type := parser_parse_type(parser)
|
||||
range.range.end = type.range.range.end
|
||||
@ -564,9 +564,6 @@ parser_is_factor_token_or_prefix :: proc(tt: TokenKind) -> bool {
|
||||
#partial switch tt {
|
||||
case .Identifier, .Integer, .Float, .String, .Character, .OpenParen:
|
||||
return true
|
||||
case .Not, .Subtract, .BitwiseNot, .Increment, .Decrement:
|
||||
// Assuming these can be used as prefixes in unary operations
|
||||
return true
|
||||
case:
|
||||
return false
|
||||
}
|
||||
|
@ -458,15 +458,26 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
|
||||
case .UnaryExpression:
|
||||
// FIXME: Verify that the operation is possible
|
||||
type_check(ast.children[0], ast)
|
||||
append(
|
||||
&g_message_list,
|
||||
message_create(
|
||||
.FIXME,
|
||||
fmt.aprintf("Check type before setting return type in unary expression"),
|
||||
ast.range,
|
||||
),
|
||||
)
|
||||
append(&g_message_list, message_create(.FIXME, fmt.aprintf("Check type in unary expression"), ast.range))
|
||||
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:
|
||||
function_return_type := scope_function_return_type_lookup()
|
||||
if function_return_type == nil {
|
||||
|
@ -1,5 +1,2 @@
|
||||
let inst := 123
|
||||
for {
|
||||
inst = inst + 1
|
||||
}
|
||||
a++
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user