From d9dfcc6354c1488e9ca1ecfdcdbd67f13621294d Mon Sep 17 00:00:00 2001 From: Niels Bross Date: Mon, 2 Sep 2024 12:34:48 +0200 Subject: [PATCH] Beginning of better API --- Lexing.jai | 19 +++++++++++++ Parsing.jai | 27 ++++++++++++++++++ module.jai | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/Lexing.jai b/Lexing.jai index 188a45e..3451623 100644 --- a/Lexing.jai +++ b/Lexing.jai @@ -500,6 +500,25 @@ scan_next_token :: (lexer : *Lexer) -> *Token { } + +lex :: (result : *Compile_Result) { + if result.had_error { + return; + } + + for file : result.files { + lexer : Lexer; + init_lexer_from_string(*lexer, file.file.source); + token : *Token = scan_next_token(*lexer); + while token && token.kind != .TOKEN_EOF { + token = scan_next_token(*lexer); + } + + // @Incomplete(nb): Temporary until we figure out a good way of passing this stuff around + copy_messages(lexer.result.messages, *result.messages); + } +} + lex :: (lexer : *Lexer, allocator : Allocator = context.allocator) -> Lexing_Result { lexer.result.tokens.allocator = allocator; token : *Token = scan_next_token(lexer); diff --git a/Parsing.jai b/Parsing.jai index 8214d00..bd51ada 100644 --- a/Parsing.jai +++ b/Parsing.jai @@ -1051,6 +1051,33 @@ declaration :: (parse_state : *Parse_State) -> *AST_Node { return decl_node; } +parse :: (result : *Compile_Result) { + for *file : result.files { + parse_state : Parse_State; + init_parse_state(*parse_state, file.tokens.tokens, file.file.path, result.allocator); + advance(*parse_state); + + if !match(*parse_state, .TOKEN_EOF) { + parse_state.result.root = make_node(*parse_state, .Program); + array_reserve(*parse_state.result.root.children, 1024); + program := parse_state.result.root; + + while !check(*parse_state, .TOKEN_EOF) { + decl := declaration(*parse_state); + if decl { + add_child(program, decl); + } + } + } + + //@Incomplete(nb): will this straight copy just work? + // Might need to rething how we do this. + file.ast_root = parse_state.result.root; + file.ast_nodes = parse_state.result.nodes; + copy_messages(parse_state.result.messages, *result.messages); + } +} + parse :: (parse_state : *Parse_State) -> Parse_Result { advance(parse_state); diff --git a/module.jai b/module.jai index 27bfbc0..ad49a7e 100644 --- a/module.jai +++ b/module.jai @@ -131,6 +131,68 @@ Shader_Variant_Collection :: struct { variants : [..]Shader_Variant; } +Input_File :: struct { + source : string; + path : string; +} + +Token_Stream :: struct { + tokens : [..]Token; +} + +Compiled_File :: struct { + file : Input_File; + tokens : Token_Stream; + ast_root : *AST_Node; + ast_nodes : [..]AST_Node; +} + +Compile_Result :: struct { + files : [..]Compiled_File; + + had_error : bool; + messages : [..]Compiler_Message; + + allocator : Allocator; + arena : Arena; +} + +//@Incomplete(niels): need to consider allocation +add_file :: (result : *Compile_Result, path : string) { + file_string, ok := read_entire_file(path); + + if !ok { + // record_error(.File_Load_Failed, "Unable to load file: %", path); + return; + } + + input_file : Input_File; + + input_file.source = file_string; + input_file.path = path; + + compiled_file : Compiled_File; + compiled_file.file = input_file; + + array_add(*result.files, compiled_file); +} + +// @Incomplete(nb): Will we ever even use this? +from_file :: (path : string) -> Compile_Result { + arr : [1]string; + arr[0] = path; + return from_files(arr); +} + +from_files :: (paths : []string) -> Compile_Result { + result : Compile_Result; + for path : paths { + add_file(*result, path); + } + + return result; +} + Compilation_Result :: struct { messages : [..]Compiler_Message; @@ -295,9 +357,24 @@ type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variabl return field; } +compile_file :: (compiler : *Shader_Compiler, paths : []string) -> Compile_Result { + result : Compile_Result; + + for path : paths { + add_file(*result, path); + } + + lex(*result); + // parse(*result); + // check(*result); + // codegen(*result); + + return result; +} + compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Result { result : Compilation_Result; - + lexer : Lexer; init_lexer_from_file(*lexer, path);