diff --git a/src/main.odin b/src/main.odin index fdf3156..82d19a4 100644 --- a/src/main.odin +++ b/src/main.odin @@ -4,42 +4,79 @@ import "core:fmt" import "core:os" main :: proc() { - handle: os.Handle - if len(os.args) >= 2 { - errno: os.Errno - handle, errno = os.open(os.args[1]) - if errno != 0 { - fmt.printf("Error opening file\n", errno) - return - } - } else { - handle = os.stdin - } - defer os.close(handle) + ctx := LLVMContextCreate() + defer LLVMContextDispose(ctx) + module := LLVMModuleCreateWithNameInContext("hello", ctx) + defer LLVMDisposeModule(module) + builder := LLVMCreateBuilderInContext(ctx) - data, err := os.read_entire_file_from_handle(handle) - if !err { - fmt.printf("Error reading file\n", err) - return + int_8_type := LLVMInt8TypeInContext(ctx) + int_8_type_ptr := LLVMPointerType(int_8_type, 0) + int_32_type := LLVMInt32TypeInContext(ctx) + + puts_function_args_type := []LLVMTypeRef{ + int_8_type_ptr, } - u8_arr : [dynamic]u8 - for ch in data { - append(&u8_arr, u8(ch)) + puts_function_type := LLVMFunctionType(int_32_type, raw_data(puts_function_args_type), 1, LLVMBool(0)) + puts_function := LLVMAddFunction(module, "puts", puts_function_type) + + main_function_type := LLVMFunctionType(int_32_type, nil, 0, LLVMBool(0)) + main_function := LLVMAddFunction(module, "main", main_function_type) + + entry := LLVMAppendBasicBlockInContext(ctx, main_function, "entry") + LLVMPositionBuilderAtEnd(builder, entry) + + puts_function_args := []LLVMValueRef { + LLVMBuildPointerCast( + builder, + LLVMBuildGlobalString(builder, "Hello world!\n", "hello"), + int_8_type_ptr, + "0", + ), } - lexer := lexer_create(&u8_arr) - parser := parser_create(lexer) + LLVMBuildCall2(builder, puts_function_type, puts_function, raw_data(puts_function_args), len(puts_function_args), "i") + LLVMBuildRet(builder, LLVMConstInt(int_32_type, 0, LLVMBool(0))) - ast := parser_parse(&parser) - type_check(ast, nil) - if len(g_message_list) > 0 { - for msg in g_message_list { - fmt.printf("%s\n", msg) - } - return - } + LLVMPrintModuleToFile(module, "hello.ll", nil) - node_print(ast) + //handle: os.Handle + //if len(os.args) >= 2 { + // errno: os.Errno + // handle, errno = os.open(os.args[1]) + // if errno != 0 { + // fmt.printf("Error opening file\n", errno) + // return + // } + //} else { + // handle = os.stdin + //} + //defer os.close(handle) + + //data, err := os.read_entire_file_from_handle(handle) + //if !err { + // fmt.printf("Error reading file\n", err) + // return + //} + + //u8_arr : [dynamic]u8 + //for ch in data { + // append(&u8_arr, u8(ch)) + //} + + //lexer := lexer_create(&u8_arr) + //parser := parser_create(lexer) + + //ast := parser_parse(&parser) + //type_check(ast, nil) + //if len(g_message_list) > 0 { + // for msg in g_message_list { + // fmt.printf("%s\n", msg) + // } + // return + //} + + //node_print(ast) }