Fix case_statement/lambda-close-over-variables bug, rename ast_node file to make ast_node:: unambigious, change test_ast to test_compiler and add a little skeleton c_generator file
This commit is contained in:
804
stdlib/ast_nodes.krak
Normal file
804
stdlib/ast_nodes.krak
Normal file
@@ -0,0 +1,804 @@
|
||||
import tree:*
|
||||
import vector:*
|
||||
import set:*
|
||||
import stack:*
|
||||
import map:*
|
||||
import util:*
|
||||
import string:*
|
||||
import mem:*
|
||||
import io:*
|
||||
|
||||
|
||||
adt ast_node {
|
||||
translation_unit: translation_unit,
|
||||
import: import,
|
||||
identifier: identifier,
|
||||
type_def: type_def,
|
||||
adt_def: adt_def,
|
||||
function: function,
|
||||
code_block: code_block,
|
||||
statement: statement,
|
||||
if_statement: if_statement,
|
||||
match_statement: match_statement,
|
||||
case_statement: case_statement,
|
||||
while_loop: while_loop,
|
||||
for_loop: for_loop,
|
||||
return_statement: return_statement,
|
||||
break_statement: break_statement,
|
||||
continue_statement: continue_statement,
|
||||
defer_statement: defer_statement,
|
||||
assignment_statement: assignment_statement,
|
||||
declaration_statement: declaration_statement,
|
||||
if_comp: if_comp,
|
||||
simple_passthrough: simple_passthrough,
|
||||
function_call: function_call,
|
||||
value: value
|
||||
}
|
||||
/*
|
||||
total:
|
||||
ASTType type;
|
||||
Type* valueType;
|
||||
Symbol symbol;
|
||||
std::map<std::string, std::vector<NodeTree<ASTData>*>> scope;
|
||||
std::set<NodeTree<ASTData>*> closedVariables;
|
||||
*/
|
||||
fun ast_node_ptr(node: ast_node): *ast_node {
|
||||
var to_ret = new<ast_node>()
|
||||
to_ret->copy_construct(&node)
|
||||
return to_ret
|
||||
}
|
||||
fun ast_translation_unit_ptr(name: string): *ast_node {
|
||||
var obj_var.construct(name): translation_unit
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::translation_unit(obj_var))
|
||||
return ptr
|
||||
}
|
||||
obj translation_unit (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var children: vector<*ast_node>
|
||||
var name: string
|
||||
fun construct(nameIn: string): *translation_unit {
|
||||
scope.construct()
|
||||
children.construct()
|
||||
name.copy_construct(&nameIn)
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *translation_unit) {
|
||||
scope.copy_construct(&old->scope)
|
||||
children.copy_construct(&old->children)
|
||||
name.copy_construct(&old->name)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
children.destruct()
|
||||
name.destruct()
|
||||
}
|
||||
fun operator=(other: ref translation_unit) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref translation_unit): bool {
|
||||
return children == other.children && name == other.name
|
||||
}
|
||||
}
|
||||
fun ast_import_ptr(name: string): *ast_node {
|
||||
var to_ret.construct(name): import
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::import(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj import (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var imported: set<string>
|
||||
var name: string
|
||||
fun construct(nameIn: string): *import {
|
||||
scope.construct()
|
||||
imported.construct()
|
||||
name.copy_construct(&nameIn)
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *import) {
|
||||
scope.copy_construct(&old->scope)
|
||||
imported.copy_construct(&old->imported)
|
||||
name.copy_construct(&old->name)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
imported.destruct()
|
||||
name.destruct()
|
||||
}
|
||||
fun operator=(other: ref import) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref import): bool {
|
||||
return imported == other.imported && name == other.name
|
||||
}
|
||||
}
|
||||
fun ast_identifier_ptr(name: *char): *ast_node {
|
||||
return ast_identifier_ptr(string(name))
|
||||
}
|
||||
fun ast_identifier_ptr(name: string): *ast_node {
|
||||
var to_ret.construct(name): identifier
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::identifier(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj identifier (Object) {
|
||||
var name: string
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(nameIn: string): *identifier {
|
||||
name.copy_construct(&nameIn)
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *identifier) {
|
||||
name.copy_construct(&old->name)
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
name.destruct()
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref identifier) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref identifier): bool {
|
||||
return name == other.name
|
||||
}
|
||||
}
|
||||
fun ast_type_def_ptr(name: string): *ast_node {
|
||||
var to_ret.construct(name): type_def
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::type_def(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj type_def (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var name: string
|
||||
fun construct(nameIn: string): *type_def {
|
||||
scope.construct()
|
||||
name.copy_construct(&nameIn)
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *type_def) {
|
||||
scope.copy_construct(&old->scope)
|
||||
name.copy_construct(&old->name)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
name.destruct()
|
||||
}
|
||||
fun operator=(other: ref type_def) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref type_def): bool {
|
||||
return name == other.name
|
||||
}
|
||||
}
|
||||
fun ast_adt_def_ptr(name: string): *ast_node {
|
||||
var to_ret.construct(name): adt_def
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::adt_def(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj adt_def (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var name: string
|
||||
fun construct(nameIn: string): *adt_def {
|
||||
scope.construct()
|
||||
name.copy_construct(&nameIn)
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *adt_def) {
|
||||
scope.copy_construct(&old->scope)
|
||||
name.copy_construct(&old->name)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
name.destruct()
|
||||
}
|
||||
fun operator=(other: ref adt_def) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref adt_def): bool {
|
||||
return name == other.name
|
||||
}
|
||||
}
|
||||
fun ast_function_ptr(): *ast_node {
|
||||
var to_ret.construct(): function
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::function(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj function (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *function {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *function) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref function) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref function): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_code_block_ptr(): *ast_node {
|
||||
var to_ret.construct(): code_block
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::code_block(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj code_block (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var children: vector<*ast_node>
|
||||
fun construct(): *code_block {
|
||||
scope.construct()
|
||||
children.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *code_block) {
|
||||
scope.copy_construct(&old->scope)
|
||||
children.copy_construct(&old->children)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
children.destruct()
|
||||
}
|
||||
fun operator=(other: ref code_block) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref code_block): bool {
|
||||
return children == other.children && scope == other.scope
|
||||
}
|
||||
}
|
||||
fun ast_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var child: *ast_node
|
||||
fun construct(): *statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
child = old->child
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref statement): bool {
|
||||
return child == other.child
|
||||
}
|
||||
}
|
||||
fun ast_if_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): if_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::if_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj if_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *if_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *if_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref if_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref if_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_match_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): match_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::match_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj match_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *match_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *match_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref match_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref match_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_case_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): case_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::case_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj case_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *case_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *case_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref case_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref case_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_while_loop_ptr(): *ast_node {
|
||||
var to_ret.construct(): while_loop
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::while_loop(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj while_loop (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *while_loop {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *while_loop) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref while_loop) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref while_loop): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_for_loop_ptr(): *ast_node {
|
||||
var to_ret.construct(): for_loop
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::for_loop(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj for_loop (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *for_loop {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *for_loop) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref for_loop) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref for_loop): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_return_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): return_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::return_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj return_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *return_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *return_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref return_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref return_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_break_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): break_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::break_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj break_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *break_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *break_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref break_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref break_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_continue_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): continue_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::continue_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj continue_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *continue_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *continue_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref continue_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref continue_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_defer_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): defer_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::defer_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj defer_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *defer_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *defer_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref defer_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref defer_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_assignment_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): assignment_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::assignment_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj assignment_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *assignment_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *assignment_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref assignment_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref assignment_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_declaration_statement_ptr(): *ast_node {
|
||||
var to_ret.construct(): declaration_statement
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::declaration_statement(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj declaration_statement (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *declaration_statement {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *declaration_statement) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref declaration_statement) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref declaration_statement): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_if_comp_ptr(): *ast_node {
|
||||
var to_ret.construct(): if_comp
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::if_comp(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj if_comp (Object) {
|
||||
var wanted_generator: string
|
||||
var statement: *ast_node
|
||||
fun construct(): *if_comp {
|
||||
wanted_generator.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *if_comp) {
|
||||
wanted_generator.copy_construct(&old->wanted_generator)
|
||||
statement = old->statement
|
||||
}
|
||||
fun destruct() {
|
||||
wanted_generator.destruct()
|
||||
}
|
||||
fun operator=(other: if_comp) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: if_comp): bool {
|
||||
return wanted_generator == other.wanted_generator && statement == other.statement
|
||||
}
|
||||
}
|
||||
fun ast_simple_passthrough_ptr(): *ast_node {
|
||||
var to_ret.construct(): simple_passthrough
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::simple_passthrough(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj simple_passthrough (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
var passthrough_str: string
|
||||
fun construct(): *simple_passthrough {
|
||||
scope.construct()
|
||||
passthrough_str.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *simple_passthrough) {
|
||||
scope.copy_construct(&old->scope)
|
||||
passthrough_str.copy_construct(&old->passthrough_str)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
passthrough_str.destruct()
|
||||
}
|
||||
fun operator=(other: ref simple_passthrough) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref simple_passthrough): bool {
|
||||
return scope == other.scope && passthrough_str == other.passthrough_str
|
||||
}
|
||||
}
|
||||
fun ast_function_call_ptr(): *ast_node {
|
||||
var to_ret.construct(): function_call
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::function_call(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj function_call (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *function_call {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *function_call) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref function_call) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref function_call): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
fun ast_value_ptr(): *ast_node {
|
||||
var to_ret.construct(): value
|
||||
var ptr = new<ast_node>()
|
||||
ptr->copy_construct(&ast_node::value(to_ret))
|
||||
return ptr
|
||||
}
|
||||
obj value (Object) {
|
||||
var scope: map<string, vector<*ast_node>>
|
||||
fun construct(): *value {
|
||||
scope.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *value) {
|
||||
scope.copy_construct(&old->scope)
|
||||
}
|
||||
fun destruct() {
|
||||
scope.destruct()
|
||||
}
|
||||
fun operator=(other: ref value) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref value): bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
||||
match (*node) {
|
||||
ast_node::translation_unit(backing) return backing.children
|
||||
ast_node::import(backing) return vector<*ast_node>()
|
||||
ast_node::identifier(backing) return vector<*ast_node>()
|
||||
ast_node::type_def(backing) return vector<*ast_node>()
|
||||
ast_node::adt_def(backing) return vector<*ast_node>()
|
||||
ast_node::function(backing) return vector<*ast_node>()
|
||||
ast_node::code_block(backing) return backing.children
|
||||
ast_node::statement(backing) return vector<*ast_node>(backing.child)
|
||||
ast_node::if_statement(backing) return vector<*ast_node>()
|
||||
ast_node::match_statement(backing) return vector<*ast_node>()
|
||||
ast_node::case_statement(backing) return vector<*ast_node>()
|
||||
ast_node::while_loop(backing) return vector<*ast_node>()
|
||||
ast_node::for_loop(backing) return vector<*ast_node>()
|
||||
ast_node::return_statement(backing) return vector<*ast_node>()
|
||||
ast_node::break_statement(backing) return vector<*ast_node>()
|
||||
ast_node::continue_statement(backing) return vector<*ast_node>()
|
||||
ast_node::defer_statement(backing) return vector<*ast_node>()
|
||||
ast_node::assignment_statement(backing) return vector<*ast_node>()
|
||||
ast_node::declaration_statement(backing) return vector<*ast_node>()
|
||||
ast_node::if_comp(backing) return vector<*ast_node>(backing.statement)
|
||||
ast_node::simple_passthrough(backing) return vector<*ast_node>()
|
||||
ast_node::function_call(backing) return vector<*ast_node>()
|
||||
ast_node::value(backing) return vector<*ast_node>()
|
||||
}
|
||||
}
|
||||
fun get_ast_name(node: *ast_node): string {
|
||||
match (*node) {
|
||||
ast_node::translation_unit(backing) return string("translation_unit: ") + backing.name
|
||||
ast_node::import(backing) return string("import: ") + backing.name + "; [" + backing.imported.reduce(fun(name: string, acc: string): string return acc + " " + name;, string()) + " ]"
|
||||
ast_node::identifier(backing) return string("identifier: ") + backing.name
|
||||
ast_node::type_def(backing) return string("type_def: ") + backing.name
|
||||
ast_node::adt_def(backing) return string("adt_def: ") + backing.name
|
||||
ast_node::function(backing) return string("function")
|
||||
ast_node::code_block(backing) return string("code_block")
|
||||
ast_node::statement(backing) return string("statement")
|
||||
ast_node::if_statement(backing) return string("if_statement")
|
||||
ast_node::match_statement(backing) return string("match_statement")
|
||||
ast_node::case_statement(backing) return string("case_statement")
|
||||
ast_node::while_loop(backing) return string("while_loop")
|
||||
ast_node::for_loop(backing) return string("for_loop")
|
||||
ast_node::return_statement(backing) return string("return_statement")
|
||||
ast_node::break_statement(backing) return string("break_statement")
|
||||
ast_node::continue_statement(backing) return string("continue_statement")
|
||||
ast_node::defer_statement(backing) return string("defer_statement")
|
||||
ast_node::assignment_statement(backing) return string("assignment_statement")
|
||||
ast_node::declaration_statement(backing) return string("declaration_statement")
|
||||
ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator
|
||||
ast_node::simple_passthrough(backing) return string("simple_passthrough: , string:") + backing.passthrough_str
|
||||
ast_node::function_call(backing) return string("function_call")
|
||||
ast_node::value(backing) return string("value")
|
||||
}
|
||||
}
|
||||
fun get_ast_scope(node: *ast_node): *map<string,vector<*ast_node>> {
|
||||
match (*node) {
|
||||
ast_node::translation_unit() return &node->translation_unit.scope
|
||||
ast_node::import() return &node->import.scope
|
||||
ast_node::identifier() return &node->identifier.scope
|
||||
ast_node::type_def() return &node->type_def.scope
|
||||
ast_node::adt_def() return &node->adt_def.scope
|
||||
ast_node::function() return &node->function.scope
|
||||
ast_node::code_block() return &node->code_block.scope
|
||||
ast_node::statement() return &node->statement.scope
|
||||
ast_node::if_statement() return &node->if_statement.scope
|
||||
ast_node::match_statement() return &node->match_statement.scope
|
||||
ast_node::case_statement() return &node->case_statement.scope
|
||||
ast_node::while_loop() return &node->while_loop.scope
|
||||
ast_node::for_loop() return &node->for_loop.scope
|
||||
ast_node::return_statement() return &node->return_statement.scope
|
||||
ast_node::break_statement() return &node->break_statement.scope
|
||||
ast_node::continue_statement() return &node->continue_statement.scope
|
||||
ast_node::defer_statement() return &node->defer_statement.scope
|
||||
ast_node::assignment_statement() return &node->assignment_statement.scope
|
||||
ast_node::declaration_statement() return &node->declaration_statement.scope
|
||||
ast_node::if_comp() return null<map<string,vector<*ast_node>>>()
|
||||
ast_node::simple_passthrough() return &node->simple_passthrough.scope
|
||||
ast_node::function_call() return &node->function_call.scope
|
||||
ast_node::value() return &node->value.scope
|
||||
}
|
||||
}
|
||||
|
||||
fun ast_to_dot(root: *ast_node): string {
|
||||
var ret = string("digraph Kaken {\n")
|
||||
var counter = 0
|
||||
var node_name_map = map<*ast_node, string>()
|
||||
var get_name = fun(node: *ast_node): string {
|
||||
if (node_name_map.contains_key(node))
|
||||
return node_name_map[node]
|
||||
var escaped = string("")
|
||||
get_ast_name(node).data.for_each(fun(c: char) {
|
||||
if (c != '"')
|
||||
escaped += c
|
||||
else
|
||||
escaped += "\\\""
|
||||
})
|
||||
escaped += to_string(counter++)
|
||||
node_name_map.set(node, escaped)
|
||||
return escaped
|
||||
}
|
||||
var helper: fun(*ast_node):void = fun(node: *ast_node) {
|
||||
get_ast_children(node).for_each(fun(child: *ast_node) {
|
||||
if (!child)
|
||||
return; // where on earth does the null come from
|
||||
ret += string("\"") + get_name(node) + "\" -> \"" + get_name(child) + "\"\n";
|
||||
helper(child)
|
||||
})
|
||||
}
|
||||
if (root)
|
||||
helper(root)
|
||||
return ret + "}"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user