Various improvements

This commit is contained in:
Slendi 2024-03-02 23:14:45 +02:00
parent 3f2ee15302
commit 64034e9086
4 changed files with 74 additions and 37 deletions

View File

@ -35,6 +35,15 @@ NodeKind :: enum {
//
If,
For,
// Done at type checking, should replace all instances of FieldAccess with these
// The value will be the FULL PATH to the field, only for module access
StructFieldAccess,
EnumFieldAccess,
UnionFieldAccess,
ModuleFieldAccess, // ModuleField$Field
// Example for std.println: std$println
}
Node :: struct {
@ -230,20 +239,47 @@ node_print :: proc(node: ^Node, indent := 0) {
}
}
parse_use_path :: proc(path: [dynamic]u8) -> (ret: [dynamic]u8) {
ret = path
// Check if the path ends with ".cat", if not, append it.
len_path := len(path)
if len(path) < 4 ||
path[len_path - 4] != '.' ||
path[len_path - 3] != 'c' ||
path[len_path - 2] != 'a' ||
path[len_path - 1] != 't' {
append(&ret, '.')
append(&ret, 'c')
append(&ret, 'a')
append(&ret, 't')
}
return
}
parse_use_path2 :: proc(path: [dynamic]u8) -> (ret: [dynamic]u8) {
ret = path
new_alias := [dynamic]u8{}
for i in 0 ..< len(path) {
if path[i] == '.' {
break
}
if path[i] == '/' {
clear(&new_alias)
continue
}
if libc.isalnum(i32(path[i])) == 0 {
append(&new_alias, '_')
} else {
append(&new_alias, path[i])
}
}
return
}
node_create_use :: proc(range: TextRange, path, alias: [dynamic]u8) -> (ret: ^Node) {
path_ := path
// Check if the path ends with ".cat", if not, append it.
len_path := len(path_)
if len(path_) < 4 ||
path_[len_path - 4] != '.' ||
path_[len_path - 3] != 'c' ||
path_[len_path - 2] != 'a' ||
path_[len_path - 1] != 't' {
append(&path_, '.')
append(&path_, 'c')
append(&path_, 'a')
append(&path_, 't')
}
path_ = parse_use_path(path)
ret = new(Node)
ret^ = {
kind = .Use,
@ -255,20 +291,7 @@ node_create_use :: proc(range: TextRange, path, alias: [dynamic]u8) -> (ret: ^No
} else {
// Get the filename, and trucate the extension, then replace any special characters with _
new_alias := [dynamic]u8{}
for i in 0 ..< len(path) {
if path[i] == '.' {
break
}
if path[i] == '/' {
clear(&new_alias)
continue
}
if libc.isalnum(i32(path[i])) == 0 {
append(&new_alias, '_')
} else {
append(&new_alias, path[i])
}
}
new_alias = parse_use_path2(path_)
if len(new_alias) == 0 {
panic("Invalid alias for use statement")
}

View File

@ -59,7 +59,19 @@ main :: proc() {
} else {
name = os.args[1]
}
module_name := main_module_name_from_filename(name)
name_dyn := [dynamic]u8{}
for ch in transmute([]u8)name {
append(&name_dyn, u8(ch))
}
module_name := parse_use_path2(name_dyn)
module_name = main_module_name_from_filename(module_name)
if len(module_name) == 0 {
clear(&module_name)
append(&module_name, 'm')
append(&module_name, 'a')
append(&module_name, 'i')
append(&module_name, 'n')
}
ctx := LLVMContextCreate()
defer LLVMContextDispose(ctx)
module := LLVMModuleCreateWithNameInContext(cstring(raw_data(module_name[:])), ctx)
@ -74,9 +86,9 @@ main :: proc() {
LLVMPrintModuleToFile(module, cstring(raw_data(module_name[:])), nil)
}
main_module_name_from_filename :: proc(fname: string) -> (module_name: [dynamic]u8) {
main_module_name_from_filename :: proc(fname: [dynamic]u8) -> (module_name: [dynamic]u8) {
temp_name := [dynamic]u8{}
for ch in transmute([]u8)fname {
for ch in fname {
if ch == '/' || ch == '\\' {
clear(&temp_name)
} else {

2
test.c
View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdint.h>
int32_t put_i32(int32_t x) {
int32_t meow(int32_t x) {
printf("%d\n", x);
return 500;
}

View File

@ -9,27 +9,29 @@
\let arr: []i32, arr2: [69]u8, ptr: ^i32
fn put_i32(val: i32) i32
fn meow(val: i32) i32
let amogus := 500 + (put_i32 8008135)
let amogus := 500 + (meow 8008135)
put_i32 69 * amogus + 420
meow 69 * amogus + 420
let a b := 5 1
if a == 5 {
if b {
put_i32 123
meow 123
} else {
put_i32 69
meow 69
}
} elif a == 2 {
put_i32 456
meow 456
} else {
put_i32 420
meow 420
}
for a != 0 {
put_i32 (1 << a)
meow (1 << a) >> 1
a = a - 1
}
meow 69 -> meow