Skip to content

Commit

Permalink
Merge pull request #16 from cimplec/comments
Browse files Browse the repository at this point in the history
Fix: single and multi line comments
  • Loading branch information
frankhart2018 authored Jul 16, 2020
2 parents 41de7e3 + 6b6c8f2 commit 5f3483d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
8 changes: 7 additions & 1 deletion compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def compile(opcodes, c_filename, table):
code = "\tif(%s) {\n" % opcode.val
# If opcode is of type exit then generate exit statement
elif opcode.type == "exit":
code = "\texit(%s);" % opcode.val
code = "\texit(%s);\n" % opcode.val
# If opcode is of type else_if then generate else if statement
elif opcode.type == "else_if":
code = "\telse if(%s) {\n" % opcode.val
Expand All @@ -306,6 +306,12 @@ 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 single_line_comment the generate single comment line
elif opcode.type == "single_line_comment":
code += "\t// %s \n" % opcode.val
# If opcode is of type multi_line_comment the generate single comment line
elif opcode.type == "multi_line_comment":
code += "/* %s*/\n" %opcode.val
# If opcode is of type switch then generate switch statement
elif opcode.type == "switch":
code += "\tswitch(" + opcode.val + ") {\n";
Expand Down
19 changes: 19 additions & 0 deletions lexical_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ def lexical_analyze(filename, table):
# Parantheses checker for detecting function call
parantheses_count = 0

# To store comment string
comment_str = ""

# Loop through the source code character by character
i = 0
while source_code[i] != "\0":
Expand Down Expand Up @@ -354,6 +357,22 @@ def lexical_analyze(filename, table):
if source_code[i + 1] == "=":
tokens.append(Token("divide_equal", "", line_num))
i += 2
# to check if it is a single line comment
elif source_code[i+1] == "/":
i += 2
while source_code[i] != "\n":
comment_str += str(source_code[i])
i += 1
tokens.append(Token("single_line_comment", comment_str, line_num))
comment_str = ""
# to check if it is a multi line comment
elif source_code[i+1] == "*":
i += 2
while source_code[i] != "*" and source_code[i+1] != "/":
comment_str += str(source_code[i])
i += 1
tokens.append(Token("multi_line_comment", comment_str, line_num))
comment_str = ""
else:
tokens.append(Token("divide", "", line_num))
i += 1
Expand Down
9 changes: 8 additions & 1 deletion simc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ def expression(
"double": "%lf",
}
for var in vars:
print(var)
_, type, _ = table.get_by_id(table.get_by_symbol(var))
if type == "var":
error("Unknown variable %s" % var, tokens[i].line_num)
Expand Down Expand Up @@ -1205,6 +1204,14 @@ def parse(tokens, table):
elif tokens[i].type == "continue":
op_codes.append(OpCode("continue", "", ""))
i += 1
# If token is of type single_line_statement then generate single_line_comment opcode
elif tokens[i].type == "single_line_comment":
op_codes.append(OpCode("single_line_comment", tokens[i].val, ""))
i += 1
# If token is of type multi_line_statement then generate multi_line_comment opcode
elif tokens[i].type == "multi_line_comment":
op_codes.append(OpCode("multi_line_comment", tokens[i].val, ""))
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)
Expand Down
26 changes: 13 additions & 13 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include <stdio.h>

int sum(int a, int b) {

return a + b;
}
/*
Code written by:- Siddhartha
Main starts
*/

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

return 0;
}
19 changes: 16 additions & 3 deletions test.simc
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
fun sum(a, b) {
return a + b
}

/*
Code written by:- Siddhartha
Main starts
*/

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

// testing single line comment

END_MAIN

0 comments on commit 5f3483d

Please sign in to comment.