Plain lambdas implemented, but not fully tested. No closures yet
This commit is contained in:
@@ -64,21 +64,25 @@ fun is_translation_unit(node: *ast_node): bool {
|
|||||||
obj translation_unit (Object) {
|
obj translation_unit (Object) {
|
||||||
var scope: map<string, vector<*ast_node>>
|
var scope: map<string, vector<*ast_node>>
|
||||||
var children: vector<*ast_node>
|
var children: vector<*ast_node>
|
||||||
|
var lambdas: vector<*ast_node>
|
||||||
var name: string
|
var name: string
|
||||||
fun construct(nameIn: string): *translation_unit {
|
fun construct(nameIn: string): *translation_unit {
|
||||||
scope.construct()
|
scope.construct()
|
||||||
children.construct()
|
children.construct()
|
||||||
|
lambdas.construct()
|
||||||
name.copy_construct(&nameIn)
|
name.copy_construct(&nameIn)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
fun copy_construct(old: *translation_unit) {
|
fun copy_construct(old: *translation_unit) {
|
||||||
scope.copy_construct(&old->scope)
|
scope.copy_construct(&old->scope)
|
||||||
children.copy_construct(&old->children)
|
children.copy_construct(&old->children)
|
||||||
|
lambdas.copy_construct(&old->lambdas)
|
||||||
name.copy_construct(&old->name)
|
name.copy_construct(&old->name)
|
||||||
}
|
}
|
||||||
fun destruct() {
|
fun destruct() {
|
||||||
scope.destruct()
|
scope.destruct()
|
||||||
children.destruct()
|
children.destruct()
|
||||||
|
lambdas.destruct()
|
||||||
name.destruct()
|
name.destruct()
|
||||||
}
|
}
|
||||||
fun operator=(other: ref translation_unit) {
|
fun operator=(other: ref translation_unit) {
|
||||||
@@ -86,7 +90,7 @@ obj translation_unit (Object) {
|
|||||||
copy_construct(&other)
|
copy_construct(&other)
|
||||||
}
|
}
|
||||||
fun operator==(other: ref translation_unit): bool {
|
fun operator==(other: ref translation_unit): bool {
|
||||||
return children == other.children && name == other.name
|
return children == other.children && name == other.name && lambdas == other.lambdas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun ast_import_ptr(name: string, translation_unit: *ast_node): *ast_node {
|
fun ast_import_ptr(name: string, translation_unit: *ast_node): *ast_node {
|
||||||
@@ -959,7 +963,7 @@ obj value (Object) {
|
|||||||
|
|
||||||
fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
fun get_ast_children(node: *ast_node): vector<*ast_node> {
|
||||||
match (*node) {
|
match (*node) {
|
||||||
ast_node::translation_unit(backing) return backing.children
|
ast_node::translation_unit(backing) return backing.children + backing.lambdas
|
||||||
ast_node::import(backing) return vector<*ast_node>()
|
ast_node::import(backing) return vector<*ast_node>()
|
||||||
ast_node::identifier(backing) return vector<*ast_node>()
|
ast_node::identifier(backing) return vector<*ast_node>()
|
||||||
ast_node::type_def(backing) return backing.variables + backing.methods
|
ast_node::type_def(backing) return backing.variables + backing.methods
|
||||||
|
|||||||
@@ -139,7 +139,10 @@ obj ast_transformation (Object) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
fun second_pass_function(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>, do_raw_template: bool): *ast_node {
|
fun second_pass_function(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>, do_raw_template: bool): *ast_node {
|
||||||
var function_name = concat_symbol_tree(get_node("func_identifier", node))
|
var func_identifier_node = get_node("func_identifier", node)
|
||||||
|
var function_name = string("lambda")
|
||||||
|
if (func_identifier_node)
|
||||||
|
function_name = concat_symbol_tree(func_identifier_node)
|
||||||
var template_dec = get_node("template_dec", node)
|
var template_dec = get_node("template_dec", node)
|
||||||
if (do_raw_template && template_dec) {
|
if (do_raw_template && template_dec) {
|
||||||
var template_types = vector<string>()
|
var template_types = vector<string>()
|
||||||
@@ -342,6 +345,8 @@ obj ast_transformation (Object) {
|
|||||||
return transform_defer_statement(node, scope, template_replacements)
|
return transform_defer_statement(node, scope, template_replacements)
|
||||||
} else if (name == "function_call") {
|
} else if (name == "function_call") {
|
||||||
return transform_function_call(node, scope, template_replacements)
|
return transform_function_call(node, scope, template_replacements)
|
||||||
|
} else if (name == "lambda") {
|
||||||
|
return transform_lambda(node, scope, template_replacements)
|
||||||
} else if (name == "boolean_expression" || name == "and_boolean_expression"
|
} else if (name == "boolean_expression" || name == "and_boolean_expression"
|
||||||
|| name == "bool_exp" || name == "expression"
|
|| name == "bool_exp" || name == "expression"
|
||||||
|| name == "shiftand" || name == "term"
|
|| name == "shiftand" || name == "term"
|
||||||
@@ -533,6 +538,13 @@ obj ast_transformation (Object) {
|
|||||||
f->function_call.parameters.for_each(fun(param: *ast_node) print(param);)
|
f->function_call.parameters.for_each(fun(param: *ast_node) print(param);)
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
fun transform_lambda(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node {
|
||||||
|
var function_node = second_pass_function(node, scope, template_replacements, false)
|
||||||
|
function_node->function.body_statement = transform_statement(get_node("statement", node), function_node, template_replacements)
|
||||||
|
while (!is_translation_unit(scope)) scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0]
|
||||||
|
scope->translation_unit.lambdas.add(function_node)
|
||||||
|
return function_node
|
||||||
|
}
|
||||||
fun transform_expression(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node return transform_expression(node, scope, search_type::none(), template_replacements)
|
fun transform_expression(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node return transform_expression(node, scope, search_type::none(), template_replacements)
|
||||||
fun transform_expression(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<string, *type>): *ast_node {
|
fun transform_expression(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<string, *type>): *ast_node {
|
||||||
var func_name = string()
|
var func_name = string()
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ obj c_generator (Object) {
|
|||||||
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
|
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
|
||||||
// iterate through children for each ast
|
// iterate through children for each ast
|
||||||
// assert translation_unit?
|
// assert translation_unit?
|
||||||
tree_pair.second->translation_unit.children.for_each(fun(child: *ast_node) {
|
(tree_pair.second->translation_unit.children + tree_pair.second->translation_unit.lambdas).for_each(fun(child: *ast_node) {
|
||||||
match (*child) {
|
match (*child) {
|
||||||
// should really check the genrator
|
// should really check the genrator
|
||||||
ast_node::if_comp(backing) {
|
ast_node::if_comp(backing) {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import simple_print: *
|
import simple_print: *
|
||||||
fun print_and_return(data: int): int {
|
|
||||||
println(data)
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
fun main(): int {
|
fun main(): int {
|
||||||
var v: fun(int):int
|
var v: fun(int):int
|
||||||
v = print_and_return
|
v = fun(data: int): int {
|
||||||
|
println(data)
|
||||||
|
return data
|
||||||
|
}
|
||||||
println(v(7))
|
println(v(7))
|
||||||
// println(print_and_return(7))
|
// println(print_and_return(7))
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user