Commit 28423723 by jneen

jneen wip module marshalling

parent 6748d45e
......@@ -362,6 +362,7 @@ function Module.new(_name, _parent, _source)
self.members = cache(function()
return List.map(_source, function(item)
print('item', inspect_value(item))
local body = tag_get(item, 1)
local head = List.head(body)
......@@ -529,10 +530,29 @@ local function compiler()
return module.compile()
end
function compile_root_module(source)
local items = source
local name = 'testy'
local first = tag_get(List.head(source), 1)
if is_tok(List.head(first), token_ids.ATMACRO) and
skel_val(List.head(first)) == 'module' and
List.size(first) == 2 then
items = List.tail(source)
name = skel_val(List.head(List.tail(first)))
end
print('name', name)
print('items', inspect_value(items))
return compile_module(name, items)
end
return {
compile_expr = compile_expr,
compile_root_expr = compile_root_expr,
compile_module = compile_module,
compile_root_module = compile_root_module,
compile_item = compile_item,
}
end
......
......@@ -27,7 +27,7 @@ local function compile_module(reader)
local errors, _ = Errors.error_scope(function()
out.skel = parse_skeleton(lexer)
if Errors.ok() then out.expanded = Macros.macro_expand(out.skel) end
if Errors.ok() then out.compiled = compiler.compile_module('testy', out.expanded) end
if Errors.ok() then out.compiled = compiler.compile_root_module(out.expanded) end
end)
return errors, out
......@@ -80,7 +80,62 @@ local function test_file()
end
_G.init = function()
test_file()
-- pass
end
local function flatten_modules(module)
local out = {}
local function go(module)
local assigns = {}
List.each(module.members, function(member)
if matches_tag(member, 'assign', 2) then
local name = tag_get(member, 0)
local body = tag_get(member, 1)
local marshalled = to_host_tree(body)
table.insert(assigns, {name=name,body=marshalled})
elseif member.name then
go(member)
end
end)
table.insert(out, {name = module.name, members = assigns})
end
go(module)
return out
end
_G.compile_file = function(file_name)
local file = io.open(file_name, 'r')
local input = file:read('*all')
local reader = Stubs.string_reader(file_name, input)
print('compiling: ', inspect_value(input))
local errors, out = compile_module(reader)
if #errors == 0 then
print('parsed: ' .. Stubs.inspect_value(out.skel))
print('expanded: ' .. Stubs.inspect_value(out.expanded))
print('compiled: ' .. Stubs.inspect_value(out.compiled))
local main_module = out.compiled
local flattened = flatten_modules(main_module)
print('flattened: ' .. Stubs.inspect_value(flattened))
return flattened
else
for _,e in pairs(errors) do
print('error: ' .. Stubs.inspect_value(e))
end
end
end
return {
......
......@@ -154,6 +154,14 @@ local function find(list, pred)
return find_map(list, function(e) if pred(e) then return e end end)
end
local function to_table(list)
local out = {}
each(list, function(el)
table.append(out, el)
end)
return out
end
Stubs.impl_inspect_tag('nil', 0, function() return '\\list()' end)
Stubs.impl_inspect_tag('cons', 2, function(head, tail)
local inspects = map(cons(head, tail), Stubs.inspect_value)
......@@ -183,6 +191,7 @@ _G.List = {
foldl = foldl,
foldr = foldr,
is_singleton = is_singleton,
to_table = to_table,
}
return List
......@@ -25,7 +25,8 @@ end
function to_host_tree(t)
if type(t) == 'table' and t.tag then
local host_values = {}
for i, v in t.values do
print('to_host_tree tag', inspect_value(t))
for i, v in pairs(t.values) do
host_values[i] = to_host_tree(v)
end
......
......@@ -4,7 +4,6 @@
#include <string.h>
#include "compiler/host.h"
#include "types/value.h"
static void stackDump (lua_State *L) {
int i;
......@@ -309,15 +308,25 @@ tulip_compiler_state* tulip_compiler_start() {
printf("lua loadtime error: %s\n", lua_tostring(lua, -1));
}
lua_getglobal(lua, "init");
return state;
}
main_file_output tulip_compiler_compile_file(tulip_compiler_state* state, char* file_name) {
lua_State *lua = state->lua_state;
lua_getglobal(lua, "compile_file");
if (lua_pcall(lua, 0, 1, 0) != 0) {
lua_pushstring(lua, file_name);
if (lua_pcall(lua, 1, 0, 0) != 0) {
printf("lua runtime error: %s\n", lua_tostring(lua, -1));
}
return state;
main_file_output o;
return o;
}
void tulip_compiler_stop(tulip_compiler_state* state) {
lua_close(state->lua_state);
}
......@@ -2,6 +2,7 @@
#include <stdbool.h>
#include <lua.h>
#include "compiler/interchange.h"
#include "types/value.h"
// character reader
......@@ -28,3 +29,4 @@ typedef struct tulip_compiler_state {
tulip_compiler_state* tulip_compiler_start();
void tulip_compiler_stop();
main_file_output tulip_compiler_compile_file(tulip_compiler_state* state, char* file_name);
......@@ -4,6 +4,7 @@
#include "runtime/state.h"
#include "runtime/transforms.h"
#include "types/core.h"
#include "compiler/interchange.h"
#include "compiler/host.h"
#include "support/options.h"
......@@ -29,22 +30,25 @@ int main(int argc, char* argv[]) {
o.heap_size = args.heap_size;
o.module_limit = args.module_limit;
tulip_runtime_state* state = tulip_runtime_start(o);
// tulip_runtime_state* state = tulip_runtime_start(o);
if (args.mode == main_file) {
tulip_compiler_state* compiler = tulip_compiler_start();
tulip_compiler_compile_file(compiler, args.main_file);
// [todo] poll main module from compiler
// [todo] catch compiler errors
main_file_output parsed;
// main_file_output parsed;
tulip_runtime_load_main(state, parsed);
// tulip_runtime_load_main(state, parsed);
// [todo] initialize logger
// // [todo] initialize logger
tulip_runtime_call_main(state, args.program_argc, args.program_argv);
// tulip_runtime_call_main(state, args.program_argc, args.program_argv);
tulip_runtime_stop(state);
// tulip_runtime_stop(state);
return 0;
......
......@@ -14,25 +14,26 @@ char* render_lambda_name(char* def_name, tulip_runtime_module* mod) {
}
LLVMValueRef tulip_runtime_transform_lambda(tulip_runtime_module* mod, char* name, tulip_runtime_ast_lambda lambda, tulip_runtime_context ctx) {
if (ctx.level == top) {
// [todo] toplevel lambda
} else {
LLVMValueRef lambda_def = LLVMAddFunction(mod->llvm_module, render_lambda_name(name, mod), tulip_lambda_type);
LLVMBasicBlockRef bb = LLVMAppendBasicBlock(lambda_def, "");
LLVMBuilderRef b = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(b, bb);
LLVMValueRef upper_scope = LLVMConstIntToPtr(LLVMConstInt(LLVMInt64Type(), (long long) ctx.local_scope, false), LLVMPointerType(LLVMVoidType(), 0));
// [todo] how to return this scope object to a closure?
LLVMValueRef scope = LLVMBuildCall(b, mod->native_defs->scope_init, (LLVMValueRef[]){upper_scope}, 1, "");
LLVMValueRef bind = LLVMGetFirstParam(lambda_def);
LLVMBuildCall(b, mod->native_defs->local_scope_set, (LLVMValueRef[]){scope, LLVMConstString(lambda.bind.name, strlen(lambda.bind.name), false)}, 2, "");
// create closure
LLVMPositionBuilderAtEnd(b, ctx.block);
// get function pointer to lambda_def
// LLVMValueRef fn = LLVMBuildCall(b, mod->native_defs->build_fnptr, (LLVMValueRef[]){lambda_def});
}
return NULL;
// if (ctx.level == top) {
// // [todo] toplevel lambda
// } else {
// LLVMValueRef lambda_def = LLVMAddFunction(mod->llvm_module, render_lambda_name(name, mod), tulip_lambda_type);
// LLVMBasicBlockRef bb = LLVMAppendBasicBlock(lambda_def, "");
// LLVMBuilderRef b = LLVMCreateBuilder();
// LLVMPositionBuilderAtEnd(b, bb);
// LLVMValueRef upper_scope = LLVMConstIntToPtr(LLVMConstInt(LLVMInt64Type(), (long long) ctx.local_scope, false), LLVMPointerType(LLVMVoidType(), 0));
// // [todo] how to return this scope object to a closure?
// LLVMValueRef scope = LLVMBuildCall(b, mod->native_defs->scope_init, (LLVMValueRef[]){upper_scope}, 1, "");
// LLVMValueRef bind = LLVMGetFirstParam(lambda_def);
// LLVMBuildCall(b, mod->native_defs->local_scope_set, (LLVMValueRef[]){scope, LLVMConstString(lambda.bind.name, strlen(lambda.bind.name), false)}, 2, "");
// // create closure
// LLVMPositionBuilderAtEnd(b, ctx.block);
// // get function pointer to lambda_def
// // LLVMValueRef fn = LLVMBuildCall(b, mod->native_defs->build_fnptr, (LLVMValueRef[]){lambda_def});
// }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment