Add support or extern function

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-02-29 09:29:21 +02:00
parent 10ae992a7b
commit 484d1a6e2c
6 changed files with 119 additions and 72 deletions

View File

@ -25,6 +25,7 @@ NodeKind :: enum {
Ret,
//
ExternFunction,
Function,
Struct,
Enum,
@ -154,6 +155,27 @@ node_create_function_call :: proc(
return
}
node_create_extern_function :: proc(
range: TextRange,
name: [dynamic]u8,
return_type: ^Node,
args: [dynamic]^Node,
) -> (
ret: ^Node,
) {
ret = new(Node)
ret^ = {
kind = .ExternFunction,
range = range,
children = {return_type},
value = name,
}
for arg in args {
append(&ret.children, arg)
}
return
}
node_create_function :: proc(
range: TextRange,
name: [dynamic]u8,

View File

@ -55,9 +55,6 @@ lexer_should_not_emit_semicolon :: proc(lexer: ^Lexer) -> bool {
lexer.last_token_kind == .OpenParen ||
lexer.last_token_kind == .OpenBrace ||
lexer.last_token_kind == .OpenBracket ||
lexer.last_token_kind == .CloseParen ||
lexer.last_token_kind == .CloseBrace ||
lexer.last_token_kind == .CloseBracket ||
lexer.last_token_kind == .Add ||
lexer.last_token_kind == .Subtract ||
lexer.last_token_kind == .Multiply ||

View File

@ -4,79 +4,86 @@ import "core:fmt"
import "core:os"
main :: proc() {
ctx := LLVMContextCreate()
defer LLVMContextDispose(ctx)
module := LLVMModuleCreateWithNameInContext("hello", ctx)
defer LLVMDisposeModule(module)
builder := LLVMCreateBuilderInContext(ctx)
// 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)
// 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_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
}
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",
),
u8_arr : [dynamic]u8
for ch in data {
append(&u8_arr, u8(ch))
}
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)))
lexer := lexer_create(&u8_arr)
parser := parser_create(lexer)
LLVMPrintModuleToFile(module, "hello.ll", nil)
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
}
//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)
node_print(ast)
}

View File

@ -185,9 +185,12 @@ parser_parse_function_definition :: proc(parser: ^Parser) -> ^Node {
}
type: ^Node = nil
if parser.tok.kind != .OpenBrace {
if parser.tok.kind != .OpenBrace && parser.tok.kind != .Semicolon {
type = parser_parse_type(parser)
}
if parser.tok.kind == .Semicolon {
return node_create_extern_function(parser.tok.range, name, type, params)
}
expect(parser, .OpenBrace)
body := parser_parse_block(parser, .CloseBrace)
return node_create_function(parser.tok.range, name, type, body, params)

View File

@ -310,6 +310,9 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
}
type_check(ast.children[1], ast)
scope_leave()
case .ExternFunction:
ast.return_type = ast_to_type(ast.children[0])
scope_stack[len(scope_stack) - 1].function_return_type = ast.return_type
case:
fmt.panicf("Unhandled node kind in type_check: {}", ast.kind)
}
@ -323,7 +326,11 @@ find_function_definitions :: proc(ast_: ^Node) -> (ret: [dynamic]^FunctionType)
if ast == nil {
continue
}
is_extern := false
#partial switch (ast.kind) {
case .ExternFunction:
is_extern := true
fallthrough
case .Function:
for fn in ret {
if compare_dyn_arrs(&fn.name, &ast.value.([dynamic]u8)) {
@ -346,8 +353,15 @@ find_function_definitions :: proc(ast_: ^Node) -> (ret: [dynamic]^FunctionType)
return_type = ast_to_type(ast.children[0])
}
for decl, i in ast.children {
if i < 2 {
continue
if is_extern == false {
if i < 2 {
continue
}
} else {
if i < 1 {
continue
}
}
type := ast_to_type(decl.children[1])
append(&fn.parameter_types, type)

View File

@ -5,6 +5,10 @@ fn name(a b: f32) i32 {
ret (a + b) as i32
}
fn put_32(str: []u32)
name 123.0 456.0
let arr: []i32, arr2: [69]u8, ptr: ^i32
put_32("Hello")