diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c829c7..5234c1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: run: | bison -d -Wcounterexamples lang.y -o lang.tab.c flex lang.l - gcc -o brainrot lang.tab.c lex.yy.c ast.c -lfl + gcc -o brainrot lang.tab.c lex.yy.c ast.c -lfl -lm - name: Upload build artifacts uses: actions/upload-artifact@v4 diff --git a/Makefile b/Makefile index ac93612..d42fbdc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: bison -d -Wcounterexamples lang.y -o lang.tab.c flex lang.l - gcc -o brainrot lang.tab.c lex.yy.c ast.c -lfl + gcc -o brainrot lang.tab.c lex.yy.c ast.c -lfl -lm test: pytest -v diff --git a/ast.c b/ast.c index 35f1890..c764155 100644 --- a/ast.c +++ b/ast.c @@ -8,6 +8,7 @@ #include #include + static JumpBuffer *jump_buffer = {0}; TypeModifiers current_modifiers = {false, false, false, false, false}; @@ -631,11 +632,13 @@ void *handle_binary_operation(ASTNode *node, int result_type) int right_type = get_expression_type(node->data.op.right); // Promote types if necessary (short -> int -> float -> double). - int promoted_type = VAR_INT; + int promoted_type = VAR_SHORT; if (left_type == VAR_DOUBLE || right_type == VAR_DOUBLE) promoted_type = VAR_DOUBLE; else if (left_type == VAR_FLOAT || right_type == VAR_FLOAT) promoted_type = VAR_FLOAT; + else if (left_type == VAR_INT || right_type == VAR_INT) + promoted_type = VAR_INT; // Allocate and evaluate operands based on promoted type. switch (promoted_type) @@ -672,6 +675,13 @@ void *handle_binary_operation(ASTNode *node, int result_type) ? (double)evaluate_expression_float(node->data.op.right) : evaluate_expression_double(node->data.op.right); break; + case VAR_SHORT: + left_value = calloc(1, sizeof(short)); + right_value = calloc(1, sizeof(short)); + *(short *)left_value = evaluate_expression_short(node->data.op.left); + *(short *)right_value = evaluate_expression_short(node->data.op.right); + break; + default: yyerror("Unsupported type promotion"); @@ -689,25 +699,22 @@ void *handle_binary_operation(ASTNode *node, int result_type) if (promoted_type == VAR_INT) *(int *)result = *(int *)left_value + *(int *)right_value; else if (promoted_type == VAR_FLOAT) - { *(float *)result = *(float *)left_value + *(float *)right_value; - } else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value + *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value + *(short *)right_value; break; case OP_MINUS: if (promoted_type == VAR_INT) *(int *)result = *(int *)left_value - *(int *)right_value; else if (promoted_type == VAR_FLOAT) - { *(float *)result = *(float *)left_value - *(float *)right_value; - } else if (promoted_type == VAR_DOUBLE) - { - volatile double res = *(double *)left_value - *(double *)right_value; - *(double *)result = res; - } + *(double *) result= *(double *)left_value - *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value - *(short *)right_value; break; @@ -718,6 +725,8 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value * *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value * *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value * *(short *)right_value; break; case OP_DIVIDE: @@ -775,6 +784,18 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(double *)result = left / right; } } + else if (promoted_type == VAR_SHORT) + { + if (*(short *)right_value == 0) + { + yyerror("Division by zero"); + *(short *)result = 0; // Define a fallback behavior for short division by zero + } + else + { + *(short *)result = *(short *)left_value / *(short *)right_value; + } + } break; case OP_MOD: if (promoted_type == VAR_INT) @@ -799,10 +820,17 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(int *)result = left % right; } } - else + else if (promoted_type == VAR_FLOAT) + { + *(float *)result = fmod(*(float *)left_value, *(float *)right_value); + } + else if (promoted_type == VAR_DOUBLE) + { + *(double *)result = fmod(*(double *)left_value, *(double *)right_value); + } + else if (promoted_type == VAR_SHORT) { - yyerror("Modulo operation is only supported for integers"); - *(int *)result = 0; + *(short *)result = *(short *)left_value % *(short *)right_value; } break; case OP_LT: @@ -812,6 +840,8 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value < *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value < *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value < *(short *)right_value; break; case OP_GT: @@ -821,6 +851,8 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value > *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value > *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value > *(short *)right_value; break; case OP_LE: @@ -830,6 +862,10 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value <= *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value <= *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value <= *(short *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value <= *(short *)right_value; break; case OP_GE: @@ -839,6 +875,10 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value >= *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value >= *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value >= *(short *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value >= *(short *)right_value; break; case OP_EQ: @@ -849,6 +889,8 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value == *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value == *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value == *(short *)right_value; break; case OP_NE: @@ -858,6 +900,8 @@ void *handle_binary_operation(ASTNode *node, int result_type) *(float *)result = *(float *)left_value != *(float *)right_value; else if (promoted_type == VAR_DOUBLE) *(double *)result = *(double *)left_value != *(double *)right_value; + else if (promoted_type == VAR_SHORT) + *(short *)result = *(short *)left_value != *(short *)right_value; break; default: @@ -1307,7 +1351,9 @@ short evaluate_expression_short(ASTNode *node) ? *(short *)result : (result_type == VAR_FLOAT) ? (short)(*(float *)result) - : (short)(*(double *)result); + : (result_type == VAR_DOUBLE) + ? (short)(*(double *)result) + : (short)(*(int *)result); free(result); return result_short; } diff --git a/examples/fizz_buzz.brainrot b/examples/fizz_buzz.brainrot index fceacfa..606739a 100644 --- a/examples/fizz_buzz.brainrot +++ b/examples/fizz_buzz.brainrot @@ -1,6 +1,6 @@ skibidi main { nut rizz i; - flex (i = 1; i <= 10; i = i + 1) { + flex (i = 1; i <= 10; i = i + 1){ edgy ( (i % 15) == 0 ) { yapping("FizzBuzz"); } amogus edgy ( (i % 3) == 0 ) { diff --git a/examples/fizz_buzz_short.brainrot b/examples/fizz_buzz_short.brainrot new file mode 100644 index 0000000..947c29e --- /dev/null +++ b/examples/fizz_buzz_short.brainrot @@ -0,0 +1,15 @@ +skibidi main { + smol i; + flex (i = 1; i <= 10; i++){ + edgy ( (i % 15) == 0 ) { + yapping("FizzBuzz"); + } amogus edgy ( (i % 3) == 0 ) { + yapping("Fizz"); + } amogus edgy ( (i % 5) == 0 ) { + yapping("Buzz"); + } amogus { + yapping("%d", i); + } + } + bussin 0; +} diff --git a/tests/expected_results.json b/tests/expected_results.json index e8dc60b..8be1287 100644 --- a/tests/expected_results.json +++ b/tests/expected_results.json @@ -10,6 +10,7 @@ "unsigned_integer_wrap": "0\n4294967294\n", "boolean": "It's valid!\nnocap: L\nnocap: W\n", "fizz_buzz": "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n", + "fizz_buzz_short": "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n", "hello_world": "Hello, World!\n", "sizeof": "4\n4\n1\n2\n4\n8\n", "char": "c\n",