if else if else if if else if else
This commit is contained in:
74
Codegen.jai
74
Codegen.jai
@@ -37,6 +37,25 @@ Codegen_Result :: struct {
|
||||
result_text : string; // @Incomplete(nb): Result for now, should likely be far more sophisticated.
|
||||
}
|
||||
|
||||
Reserved_HLSL_Words :: string.[
|
||||
"texture",
|
||||
"sampler",
|
||||
"matrix",
|
||||
"line",
|
||||
"precise",
|
||||
"shared",
|
||||
"triangle",
|
||||
"triangleadj",
|
||||
];
|
||||
|
||||
Reserved_MLSL_Words :: string.[
|
||||
""
|
||||
];
|
||||
|
||||
Reserved_GLSL_Words :: string.[
|
||||
""
|
||||
];
|
||||
|
||||
init_codegen_state :: (state : *Codegen_State, root : *AST_Node, checker_result : Semantic_Check_Result, output_language : Output_Language) {
|
||||
state.root = root;
|
||||
state.scope_stack = checker_result.scope_stack;
|
||||
@@ -50,7 +69,7 @@ indent :: (state : *Codegen_State, indentation : int) {
|
||||
for 1..indentation append(*state.builder, " ");
|
||||
}
|
||||
|
||||
dx11_type_to_string :: (type_variable : Type_Variable) -> string {
|
||||
hlsl_type_to_string :: (type_variable : Type_Variable) -> string {
|
||||
if type_variable.type == {
|
||||
case .Invalid;
|
||||
return "{{invalid}}";
|
||||
@@ -92,7 +111,7 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
|
||||
indent(state, indentation);
|
||||
|
||||
print_to_builder(*state.builder, "% ", dx11_type_to_string(field));
|
||||
print_to_builder(*state.builder, "% ", hlsl_type_to_string(field));
|
||||
|
||||
if field.struct_field_parent {
|
||||
parent_tv := from_handle(state.type_variables, field.struct_field_parent.type_variable);
|
||||
@@ -111,13 +130,19 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
print_to_builder(*state.builder, " : register(t%)", field.resource_index);
|
||||
}
|
||||
|
||||
for i :0..node.children.count - 1 {
|
||||
child := node.children[i];
|
||||
if node.children.count == 1 {
|
||||
child := node.children[0];
|
||||
|
||||
print_to_builder(*state.builder, " = ");
|
||||
emit_node(state, child, 0);
|
||||
|
||||
}
|
||||
|
||||
if node.parent.kind == .Block {
|
||||
append(*state.builder, ";");
|
||||
}
|
||||
|
||||
|
||||
for i :0..field.children.count - 1 {
|
||||
child := from_handle(state.type_variables, field.children[i]);
|
||||
emit_node(state, child.source_node, 0);
|
||||
@@ -140,7 +165,7 @@ emit_block :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
emit_node(state, statement, indentation);
|
||||
|
||||
if it_index < node.children.count {
|
||||
append(*state.builder, ";\n");
|
||||
append(*state.builder, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,7 +281,7 @@ emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, e
|
||||
|
||||
if function_variable.return_type_variable {
|
||||
return_variable := from_handle(state.type_variables, function_variable.return_type_variable);
|
||||
print_to_builder(*state.builder, "% ", dx11_type_to_string(return_variable));
|
||||
print_to_builder(*state.builder, "% ", hlsl_type_to_string(return_variable));
|
||||
} else {
|
||||
append(*state.builder, "void ");
|
||||
}
|
||||
@@ -428,6 +453,7 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
}
|
||||
case .Expression_Statement; {
|
||||
emit_node(state, node.children[0], indentation);
|
||||
append(*state.builder, ";");
|
||||
}
|
||||
case .Call; {
|
||||
emit_call(state, node, indentation);
|
||||
@@ -436,27 +462,51 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "return ");
|
||||
emit_node(state, node.children[0], 0);
|
||||
append(*state.builder, ";");
|
||||
}
|
||||
case .If; {
|
||||
indent(*state.builder, indentation);
|
||||
if node.parent.kind != .If {
|
||||
indent(*state.builder, indentation);
|
||||
}
|
||||
|
||||
append(*state.builder, "if ");
|
||||
|
||||
cond := node.children[0];
|
||||
emit_node(state, cond, 0);
|
||||
|
||||
body := node.children[1];
|
||||
emit_block(state, body, 0);
|
||||
append(*state.builder, "\n");
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "{\n");
|
||||
|
||||
if node.children.count == 2 {
|
||||
emit_else(state, node.children[2]);
|
||||
emit_block(state, body, indentation + 1);
|
||||
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "}\n");
|
||||
|
||||
if node.children.count == 3 {
|
||||
emit_else(state, node.children[2], indentation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit_else :: (state : *Codegen_State, node : *AST_Node) {
|
||||
emit_else :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "else ");
|
||||
emit_node(state, node.children[0], 0);
|
||||
|
||||
if node.kind == .If {
|
||||
emit_node(state, node, indentation);
|
||||
} else if node.kind == .Block {
|
||||
append(*state.builder, "\n");
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "{\n");
|
||||
|
||||
emit_block(state, node, indentation + 1);
|
||||
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "}");
|
||||
}
|
||||
}
|
||||
|
||||
emit_field_list :: (state : *Codegen_State, field_list : *AST_Node, indentation : int) {
|
||||
|
||||
Reference in New Issue
Block a user