Compare commits
No commits in common. "63f9eb48a4d8dfe2627788ab7c510afb9096a7c7" and "f315874236e05b8f1a047a1d98eb83b5f1ba9337" have entirely different histories.
63f9eb48a4
...
f315874236
13
Makefile
13
Makefile
@ -24,14 +24,13 @@ TESTS = $(TEST_DIR)/feature_test.rc \
|
|||||||
$(TEST_DIR)/endless_loop_test.rc \
|
$(TEST_DIR)/endless_loop_test.rc \
|
||||||
$(TEST_DIR)/math_test.rc \
|
$(TEST_DIR)/math_test.rc \
|
||||||
$(TEST_DIR)/string_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 \
|
EXAMPLES = $(EXAMPLE_DIR)/http_simple.rc \
|
||||||
$(EXAMPLE_DIR)/http_persistent.rc \
|
$(EXAMPLE_DIR)/http_persistent.rc \
|
||||||
$(EXAMPLE_DIR)/http_multi.rc
|
$(EXAMPLE_DIR)/http_multi.rc
|
||||||
|
|
||||||
.PHONY: all clean test run-tests run-examples help
|
.PHONY: all clean test run-tests run-examples help dirs
|
||||||
|
|
||||||
all: dirs $(TARGET)
|
all: dirs $(TARGET)
|
||||||
|
|
||||||
@ -41,7 +40,7 @@ dirs:
|
|||||||
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
|
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(TARGET): dirs $(OBJECTS)
|
$(TARGET): $(OBJECTS)
|
||||||
$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
|
$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@ -65,9 +64,6 @@ test: $(TARGET)
|
|||||||
@echo "Running string manipulation tests..."
|
@echo "Running string manipulation tests..."
|
||||||
@$(TARGET) $(TEST_DIR)/string_manip_test.rc 2>&1 | grep -v "Error at token" || true
|
@$(TARGET) $(TEST_DIR)/string_manip_test.rc 2>&1 | grep -v "Error at token" || true
|
||||||
@echo ""
|
@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 ==="
|
@echo "=== All Tests Completed ==="
|
||||||
|
|
||||||
run-feature-test: $(TARGET)
|
run-feature-test: $(TARGET)
|
||||||
@ -85,9 +81,6 @@ run-string-test: $(TARGET)
|
|||||||
run-string-manip: $(TARGET)
|
run-string-manip: $(TARGET)
|
||||||
$(TARGET) $(TEST_DIR)/string_manip_test.rc
|
$(TARGET) $(TEST_DIR)/string_manip_test.rc
|
||||||
|
|
||||||
run-increment-decrement-test: $(TARGET)
|
|
||||||
$(TARGET) $(TEST_DIR)/increment_decrement_test.rc
|
|
||||||
|
|
||||||
run-http-simple: $(TARGET)
|
run-http-simple: $(TARGET)
|
||||||
$(TARGET) $(EXAMPLE_DIR)/http_simple.rc
|
$(TARGET) $(EXAMPLE_DIR)/http_simple.rc
|
||||||
|
|
||||||
|
|||||||
53
TUTORIAL.md
53
TUTORIAL.md
@ -9,11 +9,6 @@ Complete guide to the RC (Retoor's C) programming language - a C-like interprete
|
|||||||
3. [Variables and Declarations](#variables-and-declarations)
|
3. [Variables and Declarations](#variables-and-declarations)
|
||||||
4. [Arrays](#arrays)
|
4. [Arrays](#arrays)
|
||||||
5. [Operators](#operators)
|
5. [Operators](#operators)
|
||||||
- [Arithmetic Operators](#arithmetic-operators)
|
|
||||||
- [Comparison Operators](#comparison-operators)
|
|
||||||
- [Logical Operators](#logical-operators)
|
|
||||||
- [Unary Operators](#unary-operators)
|
|
||||||
- [Increment and Decrement Operators](#increment-and-decrement-operators)
|
|
||||||
6. [Control Flow](#control-flow)
|
6. [Control Flow](#control-flow)
|
||||||
7. [Functions](#functions)
|
7. [Functions](#functions)
|
||||||
8. [Pointers](#pointers)
|
8. [Pointers](#pointers)
|
||||||
@ -187,54 +182,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Increment and Decrement Operators
|
|
||||||
|
|
||||||
RC supports both pre-increment/decrement (`++x`, `--x`) and post-increment/decrement (`x++`, `x--`) operators.
|
|
||||||
|
|
||||||
#### Post-increment (`x++`)
|
|
||||||
The value of the expression is the value of `x` *before* the increment.
|
|
||||||
```c
|
|
||||||
int main() {
|
|
||||||
int x = 5;
|
|
||||||
int y = x++; // y is 5, x becomes 6
|
|
||||||
printf("x = %d, y = %d\n", x, y);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Pre-increment (`++x`)
|
|
||||||
The value of the expression is the value of `x` *after* the increment.
|
|
||||||
```c
|
|
||||||
int main() {
|
|
||||||
int x = 5;
|
|
||||||
int y = ++x; // y is 6, x becomes 6
|
|
||||||
printf("x = %d, y = %d\n", x, y);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Post-decrement (`x--`)
|
|
||||||
The value of the expression is the value of `x` *before* the decrement.
|
|
||||||
```c
|
|
||||||
int main() {
|
|
||||||
int x = 5;
|
|
||||||
int y = x--; // y is 5, x becomes 4
|
|
||||||
printf("x = %d, y = %d\n", x, y);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Pre-decrement (`--x`)
|
|
||||||
The value of the expression is the value of `x` *after* the decrement.
|
|
||||||
```c
|
|
||||||
int main() {
|
|
||||||
int x = 5;
|
|
||||||
int y = --x; // y is 4, x becomes 4
|
|
||||||
printf("x = %d, y = %d\n", x, y);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Control Flow
|
## Control Flow
|
||||||
|
|
||||||
### If-Else Statements
|
### If-Else Statements
|
||||||
|
|||||||
BIN
build/globals.o
Normal file
BIN
build/globals.o
Normal file
Binary file not shown.
BIN
build/interpreter.o
Normal file
BIN
build/interpreter.o
Normal file
Binary file not shown.
BIN
build/main.o
Normal file
BIN
build/main.o
Normal file
Binary file not shown.
BIN
build/native_functions.o
Normal file
BIN
build/native_functions.o
Normal file
Binary file not shown.
BIN
build/parser.o
Normal file
BIN
build/parser.o
Normal file
Binary file not shown.
BIN
build/string_utils.o
Normal file
BIN
build/string_utils.o
Normal file
Binary file not shown.
BIN
build/tokenizer.o
Normal file
BIN
build/tokenizer.o
Normal file
Binary file not shown.
26
src/parser.c
26
src/parser.c
@ -119,17 +119,6 @@ long factor() {
|
|||||||
|
|
||||||
Symbol *sym = &locals[idx];
|
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 (pc >= MAX_TOK) return 0;
|
||||||
if (tokens[pc].type == '[') {
|
if (tokens[pc].type == '[') {
|
||||||
pc++;
|
pc++;
|
||||||
@ -169,21 +158,6 @@ long factor() {
|
|||||||
|
|
||||||
long unary() {
|
long unary() {
|
||||||
if (pc >= MAX_TOK) return 0;
|
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 == '*') {
|
if (tokens[pc].type == '*') {
|
||||||
pc++;
|
pc++;
|
||||||
int addr = unary();
|
int addr = unary();
|
||||||
|
|||||||
@ -77,8 +77,6 @@ void tokenize(char *src) {
|
|||||||
if (!strncmp(s, ">=", 2)) { t->type = Ge; s += 2; continue; }
|
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 = And; s += 2; continue; }
|
||||||
if (!strncmp(s, "||", 2)) { t->type = Or; 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 = Lt; s++; continue; }
|
||||||
if (*s == '>') { t->type = Gt; s++; continue; }
|
if (*s == '>') { t->type = Gt; s++; continue; }
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
enum {
|
enum {
|
||||||
Num = 128, Str, Id, Int, Char, Else, If, While, Return, Printf,
|
Num = 128, Str, Id, Int, Char, Else, If, While, Return, Printf,
|
||||||
Assign, Eq, Ne, Lt, Gt, Le, Ge, Or, And, Inc, Dec
|
Assign, Eq, Ne, Lt, Gt, Le, Ge, Or, And
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
@ -1,30 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user