diff --git a/Makefile b/Makefile index e3c686d..a459e6c 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,14 @@ TESTS = $(TEST_DIR)/feature_test.rc \ $(TEST_DIR)/endless_loop_test.rc \ $(TEST_DIR)/math_test.rc \ $(TEST_DIR)/string_test.rc \ - $(TEST_DIR)/string_manip_test.rc + $(TEST_DIR)/string_manip_test.rc \ + $(TEST_DIR)/increment_decrement_test.rc EXAMPLES = $(EXAMPLE_DIR)/http_simple.rc \ $(EXAMPLE_DIR)/http_persistent.rc \ $(EXAMPLE_DIR)/http_multi.rc -.PHONY: all clean test run-tests run-examples help dirs +.PHONY: all clean test run-tests run-examples help all: dirs $(TARGET) @@ -40,7 +41,7 @@ dirs: $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(CC) $(CFLAGS) -c $< -o $@ -$(TARGET): $(OBJECTS) +$(TARGET): dirs $(OBJECTS) $(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS) clean: @@ -64,6 +65,9 @@ test: $(TARGET) @echo "Running string manipulation tests..." @$(TARGET) $(TEST_DIR)/string_manip_test.rc 2>&1 | grep -v "Error at token" || true @echo "" + @echo "Running increment decrement tests..." + @$(TARGET) $(TEST_DIR)/increment_decrement_test.rc 2>&1 | grep -v "Error at token" || true + @echo "" @echo "=== All Tests Completed ===" run-feature-test: $(TARGET) @@ -81,6 +85,9 @@ run-string-test: $(TARGET) run-string-manip: $(TARGET) $(TARGET) $(TEST_DIR)/string_manip_test.rc +run-increment-decrement-test: $(TARGET) + $(TARGET) $(TEST_DIR)/increment_decrement_test.rc + run-http-simple: $(TARGET) $(TARGET) $(EXAMPLE_DIR)/http_simple.rc diff --git a/build/globals.o b/build/globals.o deleted file mode 100644 index 4b83da3..0000000 Binary files a/build/globals.o and /dev/null differ diff --git a/build/interpreter.o b/build/interpreter.o deleted file mode 100644 index fc628d6..0000000 Binary files a/build/interpreter.o and /dev/null differ diff --git a/build/main.o b/build/main.o deleted file mode 100644 index b21e492..0000000 Binary files a/build/main.o and /dev/null differ diff --git a/build/native_functions.o b/build/native_functions.o deleted file mode 100644 index 3da868b..0000000 Binary files a/build/native_functions.o and /dev/null differ diff --git a/build/parser.o b/build/parser.o deleted file mode 100644 index 17ea850..0000000 Binary files a/build/parser.o and /dev/null differ diff --git a/build/string_utils.o b/build/string_utils.o deleted file mode 100644 index 36f8098..0000000 Binary files a/build/string_utils.o and /dev/null differ diff --git a/build/tokenizer.o b/build/tokenizer.o deleted file mode 100644 index 3fa1588..0000000 Binary files a/build/tokenizer.o and /dev/null differ diff --git a/src/parser.c b/src/parser.c index c311f7c..3121107 100644 --- a/src/parser.c +++ b/src/parser.c @@ -119,6 +119,17 @@ long factor() { Symbol *sym = &locals[idx]; + if (pc < MAX_TOK && (tokens[pc].type == Inc || tokens[pc].type == Dec)) { + long val = memory[sym->addr]; + if (tokens[pc].type == Inc) { + memory[sym->addr]++; + } else { + memory[sym->addr]--; + } + pc++; + return val; + } + if (pc >= MAX_TOK) return 0; if (tokens[pc].type == '[') { pc++; @@ -158,6 +169,21 @@ long factor() { long unary() { if (pc >= MAX_TOK) return 0; + if (tokens[pc].type == Inc || tokens[pc].type == Dec) { + int op = tokens[pc++].type; + Token *t = &tokens[pc]; + if (t->type != Id) error("Expected identifier after ++/--"); + int idx = find_local(t->text, t->val); + if (idx == -1) error("Undefined variable"); + pc++; + long addr = locals[idx].addr; + if (op == Inc) { + memory[addr]++; + } else { + memory[addr]--; + } + return memory[addr]; + } if (tokens[pc].type == '*') { pc++; int addr = unary(); diff --git a/src/tokenizer.c b/src/tokenizer.c index e11e492..03de7bd 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -77,6 +77,8 @@ void tokenize(char *src) { if (!strncmp(s, ">=", 2)) { t->type = Ge; s += 2; continue; } if (!strncmp(s, "&&", 2)) { t->type = And; s += 2; continue; } if (!strncmp(s, "||", 2)) { t->type = Or; s += 2; continue; } + if (!strncmp(s, "++", 2)) { t->type = Inc; s += 2; continue; } + if (!strncmp(s, "--", 2)) { t->type = Dec; s += 2; continue; } if (*s == '<') { t->type = Lt; s++; continue; } if (*s == '>') { t->type = Gt; s++; continue; } diff --git a/src/types.h b/src/types.h index f77faf3..47bc28c 100644 --- a/src/types.h +++ b/src/types.h @@ -9,7 +9,7 @@ enum { Num = 128, Str, Id, Int, Char, Else, If, While, Return, Printf, - Assign, Eq, Ne, Lt, Gt, Le, Ge, Or, And + Assign, Eq, Ne, Lt, Gt, Le, Ge, Or, And, Inc, Dec }; typedef struct { diff --git a/tests/increment_decrement_test.rc b/tests/increment_decrement_test.rc new file mode 100644 index 0000000..914033c --- /dev/null +++ b/tests/increment_decrement_test.rc @@ -0,0 +1,30 @@ +int main() { + int a; + int b; + + a = 5; + b = a++; + printf("a = %d, b = %d\n", a, b); + + a = 5; + b = ++a; + printf("a = %d, b = %d\n", a, b); + + a = 5; + b = a--; + printf("a = %d, b = %d\n", a, b); + + a = 5; + b = --a; + printf("a = %d, b = %d\n", a, b); + + a = 5; + a++; + printf("a = %d\n", a); + + a = 5; + a--; + printf("a = %d\n", a); + + return 0; +}