diff --git a/lang/CMakeLists.txt b/lang/CMakeLists.txt index f498d0c..2522784 100644 --- a/lang/CMakeLists.txt +++ b/lang/CMakeLists.txt @@ -9,12 +9,14 @@ # Create TardisLang library project(TardisLang) add_custom_command( - OUTPUT module.c - COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/compile.py > module.c - DEPENDS compile.py + OUTPUT module_main.c + COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/compile.py + ${CMAKE_CURRENT_SOURCE_DIR}/modules/main.txt + module_main.c + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/modules/main.txt ) file(GLOB SRC_FILES "${PROJECT_SOURCE_DIR}/*.c") -add_library(TardisLang ${SRC_FILES} "module.c") +add_library(TardisLang ${SRC_FILES} "module_main.c") target_include_directories(TardisLang PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") set_property(TARGET TardisLang PROPERTY C_STANDARD 11) target_compile_options(TardisLang PRIVATE ${compile_opts}) diff --git a/lang/compile.py b/lang/compile.py index dbd98db..c3f58b8 100755 --- a/lang/compile.py +++ b/lang/compile.py @@ -2,6 +2,8 @@ # SPDX-License-Identifier: MIT # Copyright (c) 2023 John Watts and the LuminaSensum contributors +import sys + # This is a fairly basic compiler for a simple programming language # It is split to three phases: # Parsing (AST and parse sections) @@ -504,33 +506,31 @@ output += footer return output -## Tests +## Main wrapper def compile(code): lines = tokenize(code) if not lines: - return + return None ast = parse_toplevel(lines) if not ast: - return + return None ir = generate_ir(ast) if not ir: - return + return None ir_reg = registers_allocate(ir) if not ir_reg: - return - bytecode = generate_c_file(ir_reg, code) - if not bytecode: - return - print(bytecode) + return None + c_code = generate_c_file(ir_reg, code) + if not c_code: + return None + return c_code -code = """ -Function MakeNumber -Set Halfish To 618 -Set Double To Halfish Add Halfish -Set Final To Double Minus 2 -Return Final -EndFunction -""" - -compile(code) +if __name__ == "__main__": + input = open(sys.argv[1], "r") + output = open(sys.argv[2], "w") + code = input.read() + c_code = compile(code) + output.write(c_code) + input.close() + output.close() diff --git a/lang/modules/main.txt b/lang/modules/main.txt new file mode 100644 index 0000000..a3f955b --- /dev/null +++ b/lang/modules/main.txt @@ -0,0 +1,6 @@ +Function MakeNumber +Set Halfish To 618 +Set Double To Halfish Add Halfish +Set Final To Double Minus 2 +Return Final +EndFunction