Move compile result stuff out of specific stages.
This commit is contained in:
107
Parsing.jai
107
Parsing.jai
@@ -11,33 +11,22 @@
|
||||
Parse_State :: struct {
|
||||
current : *Token;
|
||||
previous : *Token;
|
||||
tokens : [..]Token;
|
||||
|
||||
current_token_index : int;
|
||||
|
||||
node_allocator : Allocator;
|
||||
node_arena : Arena;
|
||||
|
||||
child_allocator : Allocator;
|
||||
child_arena : Arena;
|
||||
|
||||
// had_error : bool;
|
||||
|
||||
path : string;
|
||||
|
||||
result : Parse_Result;
|
||||
result : *Compile_Result;
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
//@nb - Result and error handling
|
||||
Parse_Result :: struct {
|
||||
root : *AST_Node;
|
||||
nodes : [..]AST_Node;
|
||||
// Parse_Result :: struct {
|
||||
// root : *AST_Node;
|
||||
// nodes : [..]AST_Node;
|
||||
|
||||
had_error : bool;
|
||||
// had_error : bool;
|
||||
|
||||
messages : [..]Compiler_Message;
|
||||
}
|
||||
// messages : [..]Compiler_Message;
|
||||
// }
|
||||
|
||||
Parse_Error_Kind :: enum {
|
||||
Parse_Error_Type_Missing;
|
||||
@@ -129,16 +118,6 @@ parse_rules :: #run -> [(cast(int)Token_Kind.TOKEN_ERROR) + 1]Parse_Rule {
|
||||
return rules;
|
||||
}
|
||||
|
||||
init_parse_state :: (parse_state : *Parse_State, tokens : [..]Token, path : string) {
|
||||
parse_state.tokens = tokens;
|
||||
parse_state.path = path;
|
||||
parse_state.node_allocator = make_arena(*parse_state.node_arena);
|
||||
parse_state.child_allocator = make_arena(*parse_state.child_arena);
|
||||
parse_state.result.nodes.allocator = parse_state.node_allocator;
|
||||
array_reserve(*parse_state.result.nodes, 4096);
|
||||
parse_state.current_token_index = 0;
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
//@nb - Error handling functions
|
||||
|
||||
@@ -147,7 +126,7 @@ record_error :: (parse_state : *Parse_State, token : Token, message : string, re
|
||||
error : Compiler_Message;
|
||||
error.message_kind = .Error;
|
||||
error.message = message;
|
||||
error.path = parse_state.path;
|
||||
error.path = parse_state.result.file.path;
|
||||
|
||||
source_location : Source_Range;
|
||||
source_location.begin = token;
|
||||
@@ -357,7 +336,7 @@ make_node :: (parse_state : *Parse_State, kind : AST_Kind) -> *AST_Node {
|
||||
node : AST_Node;
|
||||
|
||||
node.kind = kind;
|
||||
node.children.allocator = parse_state.child_allocator;
|
||||
node.children.allocator = parse_state.result.allocator;
|
||||
array_add(*parse_state.result.nodes, node);
|
||||
|
||||
return *parse_state.result.nodes[parse_state.result.nodes.count - 1];
|
||||
@@ -393,10 +372,10 @@ advance :: (parse_state : *Parse_State) {
|
||||
parse_state.previous = parse_state.current;
|
||||
|
||||
while true {
|
||||
if parse_state.current_token_index >= parse_state.tokens.count {
|
||||
if parse_state.current_token_index >= parse_state.result.tokens.tokens.count {
|
||||
break;
|
||||
}
|
||||
parse_state.current = *parse_state.tokens[parse_state.current_token_index];
|
||||
parse_state.current = *parse_state.result.tokens.tokens[parse_state.current_token_index];
|
||||
parse_state.current_token_index += 1;
|
||||
if parse_state.current.kind != .TOKEN_ERROR break;
|
||||
|
||||
@@ -429,19 +408,24 @@ check_any :: (parse_state : *Parse_State, kinds : ..Token_Kind) -> bool {
|
||||
|
||||
//nb - Checks if the next token is of a certain kind
|
||||
check_next :: (parse_state : *Parse_State, kind : Token_Kind) -> bool {
|
||||
return parse_state.tokens[parse_state.current_token_index].kind == kind;
|
||||
return parse_state.result.tokens.tokens[parse_state.current_token_index].kind == kind;
|
||||
}
|
||||
|
||||
//nb - Consume a token if
|
||||
consume :: (parse_state : *Parse_State, kind : Token_Kind, message : string) {
|
||||
consume :: (parse_state : *Parse_State, kind : Token_Kind, message : string) {
|
||||
if parse_state.current.kind == kind {
|
||||
advance(parse_state);
|
||||
return;
|
||||
}
|
||||
|
||||
token := parse_state.previous;
|
||||
advance(parse_state);
|
||||
advance_to_sync_point(parse_state);
|
||||
unexpected_token(parse_state, token, message);
|
||||
|
||||
if parse_state.current.kind == .TOKEN_EOF {
|
||||
return;
|
||||
}
|
||||
|
||||
consume(parse_state, kind, message);
|
||||
}
|
||||
|
||||
@@ -540,8 +524,8 @@ binary :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node {
|
||||
}
|
||||
|
||||
array_access :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node {
|
||||
identifier := parse_state.tokens[parse_state.current_token_index - 3];
|
||||
left_bracket := parse_state.tokens[parse_state.current_token_index - 2];
|
||||
identifier := parse_state.result.tokens.tokens[parse_state.current_token_index - 3];
|
||||
left_bracket := parse_state.result.tokens.tokens[parse_state.current_token_index - 2];
|
||||
|
||||
array_access := make_node(parse_state, .Unary);
|
||||
array_access.token = left_bracket;
|
||||
@@ -749,7 +733,7 @@ field_declaration :: (parse_state : *Parse_State, identifier_token : *Token) ->
|
||||
node.array_field = true;
|
||||
} else {
|
||||
if !check(parse_state, .TOKEN_ASSIGN) {
|
||||
internal_error_message(*parse_state.result.messages, "Unimplemented error message.", parse_state.path);
|
||||
internal_error_message(*parse_state.result.messages, "Unimplemented error message.", parse_state.result.file.path);
|
||||
return node;
|
||||
}
|
||||
// missing_type_specifier(parse_state, identifier_token, "Expected type specifier after field name.");
|
||||
@@ -1227,51 +1211,26 @@ parse :: (result : *Compile_Result) {
|
||||
return;
|
||||
}
|
||||
|
||||
for *file : result.files {
|
||||
parse_state : Parse_State;
|
||||
init_parse_state(*parse_state, file.tokens.tokens, file.file.path);
|
||||
advance(*parse_state);
|
||||
parse_state : Parse_State;
|
||||
result.nodes.allocator = result.allocator;
|
||||
array_reserve(*result.nodes, 4096);
|
||||
parse_state.current_token_index = 0;
|
||||
parse_state.result = result;
|
||||
|
||||
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);
|
||||
|
||||
result.had_error |= parse_state.result.had_error;
|
||||
}
|
||||
}
|
||||
|
||||
parse :: (parse_state : *Parse_State) -> Parse_Result {
|
||||
advance(parse_state);
|
||||
|
||||
if !match(parse_state, .TOKEN_EOF) {
|
||||
parse_state.result.root = make_node(parse_state, .Program);
|
||||
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);
|
||||
while !check(*parse_state, .TOKEN_EOF) {
|
||||
decl := declaration(*parse_state);
|
||||
if decl {
|
||||
add_child(program, decl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parse_state.result;
|
||||
}
|
||||
|
||||
#load "AST.jai";
|
||||
|
||||
Reference in New Issue
Block a user