Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ static bool IsCharacter() {
case '*':
token.type = kTokenStar;

break;
case '%':
token.type = kTokenPercent;

break;
case '/':
token.type = kTokenSlash;
Expand Down
1 change: 1 addition & 0 deletions src/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef enum TokenType {
kTokenMinus,
kTokenStar,
kTokenSlash,
kTokenPercent,
kTokenDot,
kTokenComma,
kTokenColon,
Expand Down
7 changes: 6 additions & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ static void ParseOperator(const TokenType operation) {
case kTokenStar:
EmitByte(kOpMultiply);

break;
case kTokenPercent:
EmitByte(kOpModulo);

break;
case kTokenSlash:
EmitByte(kOpDivide);
Expand Down Expand Up @@ -343,7 +347,8 @@ static VariableType ParseTerm() {
VariableType left_type = ParseFactor();
VariableType right_type = kVariableTypeUnknown;

while (kTokenStar == token.type || kTokenSlash == token.type) {
while (kTokenStar == token.type || kTokenSlash == token.type ||
kTokenPercent == token.type) {
const TokenType kOperator = token.type;

ConsumeNextToken();
Expand Down
24 changes: 24 additions & 0 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,30 @@ void RunVm() {

break;
}
case kOpModulo: {
StackValue stack_value_one = {};
StackValue stack_value_two = {};
StackValue result = {};

// cppcheck-suppress-begin redundantInitialization
stack_value_one = Pop();
stack_value_two = Pop();
// cppcheck-suppress-end redundantInitialization

if (0 == stack_value_one.as.number) {
puts("Error: Division by zero.");

return;
}

result.as.number =
stack_value_two.as.number % stack_value_one.as.number;
result.type = kConstantTypeNumber;

Push(result);

break;
}
case kOpEquals: {
StackValue stack_value_one = {};
StackValue stack_value_two = {};
Expand Down
1 change: 1 addition & 0 deletions src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef enum Opcode {
kOpSubtract,
kOpMultiply,
kOpDivide,
kOpModulo,
kOpEquals,
kOpNotEquals,
kOpGreaterThan,
Expand Down
8 changes: 8 additions & 0 deletions tests/lexer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ void TestSlash() {
TEST_ASSERT_EQUAL_INT(kTokenSlash, token.type);
}

void TestPercent() {
FillProgramBuffer("60%3");

ConsumeNextToken(); // 60
ConsumeNextToken(); // %
TEST_ASSERT_EQUAL_INT(kTokenPercent, token.type);
}

void TestString() {
FillProgramBuffer("\"Hello, world!\"");

Expand Down
1 change: 1 addition & 0 deletions tests/lexer_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void TestPrintArithmetic();
void TestMinus();
void TestStar();
void TestSlash();
void TestPercent();
void TestString();
void TestPrintString();
void TestIdentifier();
Expand Down
2 changes: 2 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int main() {
RUN_TEST(TestMinus);
RUN_TEST(TestStar);
RUN_TEST(TestSlash);
RUN_TEST(TestPercent);
RUN_TEST(TestString);
RUN_TEST(TestPrintString);
RUN_TEST(TestIdentifier);
Expand Down Expand Up @@ -60,6 +61,7 @@ int main() {
RUN_TEST(TestSubtractArithmetic);
RUN_TEST(TestMultiplyArithmetic);
RUN_TEST(TestDivideArithmetic);
RUN_TEST(TestModuloArithmetic);
RUN_TEST(TestTrueBoolean);
RUN_TEST(TestFalseBoolean);
RUN_TEST(TestStringParse);
Expand Down
2 changes: 2 additions & 0 deletions tests/parser_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void TestMultiplyArithmetic() {

void TestDivideArithmetic() { TestBinaryOperator("print(10 / 5)", kOpDivide); }

void TestModuloArithmetic() { TestBinaryOperator("print(10 % 5)", kOpModulo); }

void TestLessThanCondition() {
TestBinaryOperator("print(1 < 10)", kOpLessThan);
}
Expand Down
1 change: 1 addition & 0 deletions tests/parser_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void TestAddArithmetic();
void TestSubtractArithmetic();
void TestMultiplyArithmetic();
void TestDivideArithmetic();
void TestModuloArithmetic();
void TestTrueBoolean();
void TestFalseBoolean();
void TestStringParse();
Expand Down