Exemple #1
0
def generate_code(path):
    msg = ""
    line_no = 0
    lexer = Lexer().get_lexer()
    pg = Parser()
    pg.parse()
    parser = pg.get_parser()
    with open(path) as fp:
        line_no = line_no + 1
        for line in fp:
            try:
                if ((len(line.replace('\n', "")) > 0) and ('#' not in line)):
                    token = lexer.lex(line.replace('\n', ""))
                    parser.parse(token)
                line_no = line_no + 1
            except:
                msg = "Syntax error in line No " + str(line_no)
                return msg
    fp.close()
    Code = Codegen()
    write_file(Code.doit(), path)
    return "Successfully generated"
Exemple #2
0
from lex import Lexer
from parse import Parser
from codegen import CodeGen

fname = "input.toy"
with open(fname) as f:
    text_input = f.read()

lexer = Lexer().get_lexer()
tokens = lexer.lex(text_input)

codegen = CodeGen()

module = codegen.module
builder = codegen.builder
printf = codegen.printf

#~ pg = Parser(module, builder, printf)
pg1 = Parser()
pg1.parse()
parser = pg1.get_parser()
parser.parse(tokens)

codegen.create_ir()
#~ codegen.save_ir("output.ll")
Exemple #3
0
from lexer import Lexer
from parse import Parser
from codegen import CodeGen

text_input = """
print(4 + 4 - 2);
"""

lexer = Lexer().get_lexer()
tokens = lexer.lex(text_input)

codegen = CodeGen()

module = codegen.module
builder = codegen.builder
printf = codegen.printf

pg = Parser(module, builder, printf)
pg.parse()
parser = pg.get_parser()
parser.parse(tokens).eval()

codegen.create_ir()
codegen.save_ir("output.ll")