100 lines
2.4 KiB
Odin
100 lines
2.4 KiB
Odin
package main
|
|
|
|
import "core:fmt"
|
|
import "core:os"
|
|
|
|
main :: proc() {
|
|
// ctx := LLVMContextCreate()
|
|
// defer LLVMContextDispose(ctx)
|
|
// module := LLVMModuleCreateWithNameInContext("hello", ctx)
|
|
// defer LLVMDisposeModule(module)
|
|
// builder := LLVMCreateBuilderInContext(ctx)
|
|
|
|
// 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,
|
|
// }
|
|
|
|
// 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",
|
|
// ),
|
|
// }
|
|
|
|
// 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)))
|
|
|
|
// LLVMPrintModuleToFile(module, "hello.ll", nil)
|
|
|
|
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)
|
|
if len(g_message_list) > 0 {
|
|
for msg in g_message_list {
|
|
fmt.printf("%s\n", msg)
|
|
}
|
|
return
|
|
}
|
|
clear(&g_message_list)
|
|
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)
|
|
|
|
ctx := LLVMContextCreate()
|
|
defer LLVMContextDispose(ctx)
|
|
module := LLVMModuleCreateWithNameInContext("hello", ctx)
|
|
defer LLVMDisposeModule(module)
|
|
builder := LLVMCreateBuilderInContext(ctx)
|
|
|
|
generate_llvm(ctx, module, builder, ast)
|
|
|
|
LLVMPrintModuleToFile(module, "hello.ll", nil)
|
|
}
|
|
|