From 7648643b7f25b7752aacef9e758cf28372529ab4 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 12 Jun 2023 19:52:17 +0200 Subject: postfix operators are gucci --- samples/helloworld.golem | 2 +- src/main.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++-- testing.sh | 5 ++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/samples/helloworld.golem b/samples/helloworld.golem index 2e0d82c..37698c0 100644 --- a/samples/helloworld.golem +++ b/samples/helloworld.golem @@ -10,7 +10,7 @@ main() { i = 0; loop { - i = i + 1; + i++; if (i > iterations) break; print(str); diff --git a/src/main.c b/src/main.c index 6337b1d..de84ea4 100644 --- a/src/main.c +++ b/src/main.c @@ -90,6 +90,8 @@ is_punct(const char *p) { if (strchr("=!<>", p[0]) != NULL && p[1] == '=') return 2; + if (strchr("-+", p[0]) != NULL && p[0] == p[1]) + return 2; return (strchr("+-/*()<>,;{}[]=&^|", p[0]) != NULL); } @@ -198,6 +200,7 @@ typedef enum { NOD_DBG_STMT, NOD_WRT_STMT, NOD_ASSIGN_STMT, + NOD_POSTFIX_STMT, NOD_IFELSE_STMT, NOD_LOOP_STMT, NOD_FUN, @@ -280,8 +283,6 @@ new_node(NodeType type) static Node * new_word(Token *tok) { - expect(tok, TOK_WORD); - Node *const node = new_node(NOD_WORD); node->loc = tok->loc; node->len = tok->len; @@ -324,6 +325,7 @@ static Node *break_stmt(Token **rest, Token *tok); static Node *slp_stmt(Token **rest, Token *tok); static Node *dbg_stmt(Token **rest, Token *tok); static Node *wrt_stmt(Token **rest, Token *tok); +static Node *postfix_stmt(Token **rest, Token *tok); static Node *assign_stmt(Token **rest, Token *tok); static Node *ifelse_stmt(Token **rest, Token *tok); static Node *loop_stmt(Token **rest, Token *tok); @@ -652,6 +654,8 @@ stmt(Token **rest, Token *tok) } if (equal(tok->next, "=")) return assign_stmt(rest, tok); + if (equal(tok->next, "++") || equal(tok->next, "--")) + return postfix_stmt(rest, tok); return expr_stmt(rest, tok); } @@ -727,6 +731,19 @@ wrt_stmt(Token **rest, Token *tok) return node; } +static Node * +postfix_stmt(Token **rest, Token *tok) +{ + Node *lhs; + expect(tok, TOK_WORD); + lhs = new_word(tok); + tok = tok->next; + expect(tok, TOK_PUNCT); + Node *const node = new_binary(NOD_POSTFIX_STMT, lhs, new_word(tok)); + *rest = skip(tok->next, ";"); + return node; +} + static Node * assign_stmt(Token **rest, Token *tok) { @@ -941,6 +958,38 @@ gen_assign_stmt(Node *node) } } +static void +gen_postfix_stmt(Node *node) +{ + const int found = node_find(locals, node->lhs); + + if (found != -1) { + printf("\tLIT ,__stack_ptr LDA\n"); + if (locals_size - found - 1) + printf("\tLIT %04x SUB\n", + locals_size - found - 1); + } else + printf("\tLIT ,__gl_%.*s\n", + node->lhs->len, node->lhs->loc); + printf("\tLDA\n"); + + if (node_equal(node->rhs, "++")) + printf("\tINC\n"); + else if (node_equal(node->rhs, "--")) + printf("\tDEC\n"); + + if (found != -1) { + printf("\tLIT ,__stack_ptr LDA\n"); + if (locals_size - found - 1) + printf("\tLIT %04x SUB\n", + locals_size - found - 1); + } else + printf("\tLIT ,__gl_%.*s\n", + node->lhs->len, node->lhs->loc); + + printf("\tSTA\n"); +} + static void gen_stmt(Node *node, Node *fname, int break_lbl) { @@ -989,6 +1038,9 @@ gen_stmt(Node *node, Node *fname, int break_lbl) case NOD_ASSIGN_STMT: gen_assign_stmt(node); break; + case NOD_POSTFIX_STMT: + gen_postfix_stmt(node); + break; case NOD_IFELSE_STMT: lbl = label; label += 1 + (node->rhs->next != NULL); diff --git a/testing.sh b/testing.sh index 6db3877..b34667e 100755 --- a/testing.sh +++ b/testing.sh @@ -1,6 +1,6 @@ #!/bin/sh -function test() { +test() { QUIET="$1" CODE="$2" echo "--- $CODE ---" @@ -52,4 +52,7 @@ test "$1" "main() { local a; dbg a; inc(&a); dbg a; } inc(p) { [p] = [p] + 1; }" test "$1" "global a; main() { dbg a; inc(&a); dbg a; } inc(p) { [p] = [p] + 1; }" test "$1" "main() 0; //ayayayayayayaya comment" test "$1" "main() return;" +test "$1" "main() { local a; a = 5; dbg a; a++; dbg a; }" +test "$1" "main() { local a; a = 5; dbg a; a--; dbg a; }" +exit rm -f build/tmp.* -- cgit v1.2.3