code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(108 lines)
1 -- This file contains a set of compile actions needed for bootstrapping
2 {:match, :sub, :gsub, :format, :byte, :find} = string
3 {:LuaCode, :Source} = require "code_obj"
4 SyntaxTree = require "syntax_tree"
5 Files = require "files"
7 {:fail_at} = require('nomsu_compiler')
9 MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
10 compile_actions = {
11 [""]: (_t, fn, ...)=>
12 lua = LuaCode!
13 fn_lua = @compile(fn)
14 lua\add fn_lua
15 unless fn_lua\text!\match("^%(.*%)$") or fn_lua\text!\match("^[_a-zA-Z][_a-zA-Z0-9.]*$")
16 lua\parenthesize!
17 lua\add "("
18 for i=1,select('#',...)
19 lua\add(", ") if i > 1
20 lua\add @compile((select(i, ...)))
21 lua\add ")"
22 return lua
24 ["Lua"]: (_t, code)=>
25 if not code
26 return LuaCode("LuaCode()")
27 if code.type != "Text"
28 return LuaCode("LuaCode:from(", tostring(code.source)\as_lua!, ", ", @compile(code), ")")
30 operate_on_text = (text)->
31 lua = LuaCode\from(text.source, "LuaCode:from(", tostring(text.source)\as_lua!)
32 for bit in *text
33 local bit_lua
34 if type(bit) == "string"
35 bit_lua = bit\as_lua!
36 elseif bit.type == "Text"
37 bit_lua = operate_on_text(bit)
38 elseif bit.type == "Block"
39 bit_lua = LuaCode\from bit.source, "(function()",
40 "\n local _lua = LuaCode:from(", tostring(bit.source)\as_lua!, ")",
41 "\n local function add(...) _lua:add(...) end",
42 "\n local function join_with(glue)",
43 "\n local old_bits = _lua.bits",
44 "\n _lua = LuaCode:from(_lua.source)",
45 "\n _lua:concat_add(old_bits, glue)",
46 "\n end",
47 "\n ", @compile(bit),
48 "\n return _lua",
49 "\nend)()"
50 else
51 bit_lua = @compile(bit)
53 bit_leading_len = #(bit_lua\match("^[^\n]*"))
54 lua\add(lua\trailing_line_len! + bit_leading_len > MAX_LINE and ",\n " or ", ")
55 lua\add(bit_lua)
56 lua\add ")"
57 return lua
59 return operate_on_text code
61 ["lua >"]: (_t, code)=>
62 if code.type != "Text"
63 return code
64 operate_on_text = (text)->
65 lua = LuaCode\from(text.source)
66 for bit in *text
67 if type(bit) == "string"
68 lua\add bit
69 elseif bit.type == "Text"
70 lua\add(operate_on_text(bit))
71 else
72 lua\add @compile(bit)
73 return lua
74 return operate_on_text code
76 ["= lua"]: (_t, code)=>
77 @compile(SyntaxTree{type:"Action", "lua", ">", code})
79 ["1 as lua"]: (_t, code)=>
80 LuaCode("_ENV:compile(", @compile(code), ")")
82 ["use"]: (_t, path)=>
83 LuaCode("_ENV:use(#{@compile(path)})")
85 ["export"]: (_t, path)=>
86 LuaCode("_ENV:export(#{@compile(path)})")
88 ["run"]: (_t, path)=>
89 LuaCode("_ENV:run(#{@compile(path)})")
91 ["test"]: (_t, body)=>
92 unless body.type == 'Block'
93 fail_at(body, "Compile error: This should be a Block")
94 test_nomsu = body\get_source_code!\match(":[ ]*(.*)")
95 if indent = test_nomsu\match("\n([ ]*)")
96 test_nomsu = test_nomsu\gsub("\n"..indent, "\n")
97 test_text = @compile(SyntaxTree{type:"Text", source:body.source, test_nomsu})
98 return LuaCode "TESTS[#{tostring(body.source)\as_lua!}] = ", test_text
100 ["is jit"]: (_t, code)=> LuaCode("jit")
101 ["nomsu environment"]: (_t)=> LuaCode("_ENV")
102 ["nomsu environment name"]: (_t)=> LuaCode('"_ENV"')
103 ["Lua version"]: (_t)=> LuaCode("LUA_API")
104 ["this file was run directly"]: (_t)=> LuaCode('WAS_RUN_DIRECTLY')
105 ["the command line arguments"]: (_t)=> LuaCode('COMMAND_LINE_ARGS')
108 return compile_actions