Skip to content

Commit

Permalink
Add switch functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
frankhart2018 committed Jul 15, 2020
1 parent c3a9adc commit 0c204bf
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
9 changes: 9 additions & 0 deletions compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ def compile(opcodes, c_filename, table):
# If opcode is of type continue then generate continue statement
elif opcode.type == "continue":
code += "\tcontinue;\n"
# If opcode is of type switch then generate switch statement
elif opcode.type == "switch":
code += "\tswitch(" + opcode.val + ") {\n";
# If opcode is of type case then generate case statement
elif opcode.type == "case":
code += "\tcase " + opcode.val + ":\n"
# If opcode is of type default then generate default statement
elif opcode.type == "default":
code += "\tdefault:\n"

outside_code, ccode = compile_func_main_code(
outside_code, ccode, outside_main, code
Expand Down
8 changes: 8 additions & 0 deletions lexical_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def is_keyword(value):
"continue",
"input",
"exit",
"switch",
"case",
"default"
]


Expand Down Expand Up @@ -353,6 +356,11 @@ def lexical_analyze(filename, table):
tokens.append(Token("less_than_equal", "", line_num))
i += 2

# Identifiying colon token
elif source_code[i] == ':':
tokens.append(Token("colon", "", line_num))
i += 1

# Otherwise increment the index
else:
i += 1
Expand Down
3 changes: 3 additions & 0 deletions simc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
# Get opcodes from parser
op_codes = parse(tokens, table)

for op_code in op_codes:
print(op_code)

# Compile to C code
compile(op_codes, c_filename, table)
37 changes: 37 additions & 0 deletions simc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,30 @@ def exit_statement(tokens, i, table, func_ret_type):

return OpCode("exit", op_value[:-1]), i, func_ret_type

def switch_statement(tokens, i, table, func_ret_type):

check_if(tokens[i].type, "left_paren", "Expected ( after switch", tokens[i].line_num)

op_value, _, i, func_ret_type = expression(
tokens, i+1, table, "Expected expression inside switch statement", func_ret_type=func_ret_type
)

check_if(tokens[i-1].type, "right_paren", "Expected ) after expression in switch", tokens[i-1].line_num)

check_if(tokens[i+1].type, "left_brace", "Expected { after switch statement", tokens[i].line_num)

return OpCode("switch", op_value[:-1], ""), i+1, func_ret_type

def case_statement(tokens, i, table, func_ret_type):

op_value, _, i, func_ret_type = expression(
tokens, i, table, "Expected expected expression after case", expect_paren=False, func_ret_type=func_ret_type
)

check_if(tokens[i].type, "colon", "Expected : after case in switch statement", tokens[i].line_num)

return OpCode("case", op_value, ""), i+1, func_ret_type

def parse(tokens, table):
"""
Parse tokens and generate opcodes
Expand Down Expand Up @@ -1127,6 +1151,19 @@ def parse(tokens, table):
elif tokens[i].type == "continue":
op_codes.append(OpCode("continue", "", ""))
i += 1
# If token is of type switch then generate switch opcode
elif tokens[i].type == "switch":
switch_opcode, i, func_ret_type = switch_statement(tokens, i+1, table, func_ret_type)
op_codes.append(switch_opcode)
# If token is of type case then generate case opcode
elif tokens[i].type == "case":
case_opcode, i, func_ret_type = case_statement(tokens, i+1, table, func_ret_type)
op_codes.append(case_opcode)
# If token is of type default then generate default opcode
elif tokens[i].type == "default":
check_if(tokens[i+1].type, "colon", "Expected : after default statement in switch", tokens[i+1].line_num)
op_codes.append(OpCode("default", "", ""))
i += 2
# Otherwise increment the index
else:
i += 1
Expand Down
22 changes: 14 additions & 8 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include <stdio.h>

int sum(int a, int b) {

return a + b;
}

int main() {
int hello = sum(1, 2);
printf("The sum = %d", hello);
exit(0);
int a = 1;
int b = 2;
switch(a + b) {
case 1:
printf("Hello");
break;
case 2:
printf("World");
break;
default:
printf("Default");
break;
}

return 0;
}
21 changes: 14 additions & 7 deletions test.simc
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
fun sum(a, b) {
return a + b
}

MAIN
var hello = sum(1, 2)
print("The sum = {hello}")
exit (0)
var a = 1
var b = 2

switch(a + b) {
case 1:
print("Hello")
break
case 2:
print("World")
break
default:
print("Default")
break
}
END_MAIN

0 comments on commit 0c204bf

Please sign in to comment.