diff --git a/compiler.py b/compiler.py index b0e1663..d2f8d21 100644 --- a/compiler.py +++ b/compiler.py @@ -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 @@ -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"; diff --git a/lexical_analyzer.py b/lexical_analyzer.py index 214cb4e..6286658 100644 --- a/lexical_analyzer.py +++ b/lexical_analyzer.py @@ -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": @@ -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 diff --git a/simc_parser.py b/simc_parser.py index 2e6e37d..0da341a 100644 --- a/simc_parser.py +++ b/simc_parser.py @@ -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) @@ -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) diff --git a/test.c b/test.c index 13dc893..8f1e634 100644 --- a/test.c +++ b/test.c @@ -1,19 +1,19 @@ #include +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; } diff --git a/test.simc b/test.simc index 0050f34..f64d199 100644 --- a/test.simc +++ b/test.simc @@ -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 +