aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--code_obj.lua15
-rw-r--r--code_obj.moon11
-rw-r--r--core/metaprogramming.nom25
-rw-r--r--nomsu_compiler.lua3
-rw-r--r--nomsu_compiler.moon1
5 files changed, 30 insertions, 25 deletions
diff --git a/code_obj.lua b/code_obj.lua
index 84af36a..edbd446 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -3,6 +3,8 @@ do
local _obj_0 = table
insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
end
+local repr
+repr = require('utils').repr
local LuaCode, NomsuCode, Source
do
local _class_0
@@ -77,6 +79,7 @@ local Code
do
local _class_0
local _base_0 = {
+ is_code = true,
append = function(self, ...)
local n = select("#", ...)
local bits, indents = self.bits, self.indents
@@ -86,14 +89,15 @@ do
repeat
local b = select(i, ...)
assert(b, "code bit is nil")
- if Source:is_instance(b) then
- require('ldt').breakpoint()
- end
+ assert(not Source:is_instance(b), "code bit is a Source")
if b == '' then
_continue_0 = true
break
end
bits[#bits + 1] = b
+ if type(b) ~= 'string' and not (type(b) == 'table' and b.is_code) then
+ b = repr(b)
+ end
if type(b) == 'string' then
local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
if trailing_text then
@@ -415,6 +419,11 @@ do
lua.is_value = true
return lua
end
+ self.Comment = function(...)
+ local lua = LuaCode(...)
+ lua.is_comment = true
+ return lua
+ end
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
diff --git a/code_obj.moon b/code_obj.moon
index ce8e715..caca4cf 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -2,6 +2,7 @@
-- build up generated code, while keeping track of where it came from, and managing
-- indentation levels.
{:insert, :remove, :concat} = table
+{:repr} = require 'utils'
local LuaCode, NomsuCode, Source
export LINE_STARTS
@@ -44,6 +45,7 @@ class Source
return Source(@filename, @start+offset, @stop)
class Code
+ is_code: true
new: (@source, ...)=>
@bits = {}
@indents, @current_indent = {}, 0
@@ -60,9 +62,11 @@ class Code
for i=1,n
b = select(i, ...)
assert(b, "code bit is nil")
- if Source\is_instance(b) then require('ldt').breakpoint!
+ assert(not Source\is_instance(b), "code bit is a Source")
if b == '' then continue
bits[#bits+1] = b
+ if type(b) != 'string' and not (type(b) == 'table' and b.is_code)
+ b = repr(b)
if type(b) == 'string'
trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
if trailing_text
@@ -131,6 +135,11 @@ class LuaCode extends Code
lua = LuaCode(...)
lua.is_value = true
return lua
+
+ @Comment = (...)->
+ lua = LuaCode(...)
+ lua.is_comment = true
+ return lua
add_free_vars: (vars)=>
return unless #vars > 0
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 5a80c64..631a869 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -85,8 +85,6 @@ compile [local action %actions %body] to
end
return lua
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
compile [action %actions %body] to
lua> ".."
local lua = \(compile as: local action %actions %body)
@@ -96,8 +94,6 @@ compile [action %actions %body] to
compile [action %action] to
Lua value "A\(%action.stub as lua id)"
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
compile [parse %actions as %body] to
lua> ".."
local replacements = {}
@@ -123,14 +119,8 @@ compile [parse %actions as %body] to
local ret = \(compile as: compile %actions to %new_body)
return ret
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-lua> ".."
- COMPILE_ACTIONS["% as lua expr"] = function(nomsu, tree, _t)
- return LuaCode.Value(tree.source, "nomsu:compile(", nomsu:compile(_t):as_expr(), "):as_expr()")
- end
-#
- compile [%tree as lua expr] to
- Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree):as_expr()")):as_expr()"
+compile [%tree as lua expr] to
+ Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree):as_expr()")):as_expr()"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compile [%tree as lua] to
@@ -159,8 +149,6 @@ action [%var as lua identifier, %var as lua id]
elseif \%var.type == 'Action' then return "A"..string.as_lua_id(\%var.stub)
end
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
compile [% is syntax tree] to
Lua value "AST.is_syntax_tree(\(% as lua expr))"
@@ -201,8 +189,6 @@ compile [quote %s] to
repr(\(%s as lua expr))
compile [type of %obj] to: Lua value "type(\(%obj as lua expr))"
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
compile [parse %text] to
Lua value ".."
nomsu:parse(NomsuCode("\("\(%text.source)")", \(%text as lua expr)))
@@ -212,7 +198,7 @@ compile [parse %text from %filename] to
nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(%text as lua expr)))
compile [run %nomsu_code] to
- Lua value "nomsu:run(NomsuCode(\(quote "\(%nomsu_code.source)"), \(%nomsu_code as lua expr)))"
+ Lua value "nomsu:run(\(%nomsu_code as lua expr), \(=lua "repr(tostring(\(%nomsu_code.source)))"))"
action [run tree %tree, %tree as value]
lua> ".."
@@ -236,8 +222,7 @@ compile [core version] to: Lua value "NOMSU_CORE_VERSION"
compile [lib version] to: Lua value "NOMSU_LIB_VERSION"
compile [command line args] to: Lua value "arg"
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
+~~~~
compile [with local compile actions %body] to
Lua ".."
do
@@ -245,8 +230,6 @@ compile [with local compile actions %body] to
\(%body as lua statements)
end
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
action [Nomsu version]
use "lib/version.nom"
return "\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)"
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index 1de8fbb..ada6c12 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -355,6 +355,9 @@ do
source = nil
end
source = source or (to_run.source or Source(to_run, 1, #to_run))
+ if type(source) == 'string' then
+ source = Source:from_string(source)
+ end
if not files.read(source.filename) then
files.spoof(source.filename, to_run)
end
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index dd3000e..283fa52 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -231,6 +231,7 @@ with NomsuCompiler
.run = (to_run, source=nil)=>
source or= to_run.source or Source(to_run, 1, #to_run)
+ if type(source) == 'string' then source = Source\from_string(source)
if not files.read(source.filename) then files.spoof(source.filename, to_run)
tree = if AST.is_syntax_tree(to_run) then to_run else @parse(to_run, source)
if tree == nil -- Happens if pattern matches, but there are no captures, e.g. an empty string