Skip to content

Commit

Permalink
Merge pull request #69 from Brainrotlang/fix/smol
Browse files Browse the repository at this point in the history
Fix(smol): adding binary expression for short type
  • Loading branch information
araujo88 authored Jan 16, 2025
2 parents 84a5451 + dc940cb commit c0dab0a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
72 changes: 59 additions & 13 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdint.h>
#include <stdio.h>


static JumpBuffer *jump_buffer = {0};

TypeModifiers current_modifiers = {false, false, false, false, false};
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand All @@ -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;

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fizz_buzz.brainrot
Original file line number Diff line number Diff line change
@@ -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 ) {
Expand Down
15 changes: 15 additions & 0 deletions examples/fizz_buzz_short.brainrot
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions tests/expected_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c0dab0a

Please sign in to comment.