Minor changes.

This commit is contained in:
Bruce Hill 2018-07-13 14:30:32 -07:00
parent 84c0058e69
commit 01a4f36398
5 changed files with 30 additions and 25 deletions

View File

@ -3,6 +3,8 @@ do
local _obj_0 = table local _obj_0 = table
insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
end end
local repr
repr = require('utils').repr
local LuaCode, NomsuCode, Source local LuaCode, NomsuCode, Source
do do
local _class_0 local _class_0
@ -77,6 +79,7 @@ local Code
do do
local _class_0 local _class_0
local _base_0 = { local _base_0 = {
is_code = true,
append = function(self, ...) append = function(self, ...)
local n = select("#", ...) local n = select("#", ...)
local bits, indents = self.bits, self.indents local bits, indents = self.bits, self.indents
@ -86,14 +89,15 @@ do
repeat repeat
local b = select(i, ...) local b = select(i, ...)
assert(b, "code bit is nil") assert(b, "code bit is nil")
if Source:is_instance(b) then assert(not Source:is_instance(b), "code bit is a Source")
require('ldt').breakpoint()
end
if b == '' then if b == '' then
_continue_0 = true _continue_0 = true
break break
end end
bits[#bits + 1] = b 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 if type(b) == 'string' then
local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$") local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
if trailing_text then if trailing_text then
@ -415,6 +419,11 @@ do
lua.is_value = true lua.is_value = true
return lua return lua
end end
self.Comment = function(...)
local lua = LuaCode(...)
lua.is_comment = true
return lua
end
if _parent_0.__inherited then if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0) _parent_0.__inherited(_parent_0, _class_0)
end end

View File

@ -2,6 +2,7 @@
-- build up generated code, while keeping track of where it came from, and managing -- build up generated code, while keeping track of where it came from, and managing
-- indentation levels. -- indentation levels.
{:insert, :remove, :concat} = table {:insert, :remove, :concat} = table
{:repr} = require 'utils'
local LuaCode, NomsuCode, Source local LuaCode, NomsuCode, Source
export LINE_STARTS export LINE_STARTS
@ -44,6 +45,7 @@ class Source
return Source(@filename, @start+offset, @stop) return Source(@filename, @start+offset, @stop)
class Code class Code
is_code: true
new: (@source, ...)=> new: (@source, ...)=>
@bits = {} @bits = {}
@indents, @current_indent = {}, 0 @indents, @current_indent = {}, 0
@ -60,9 +62,11 @@ class Code
for i=1,n for i=1,n
b = select(i, ...) b = select(i, ...)
assert(b, "code bit is nil") 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 if b == '' then continue
bits[#bits+1] = b bits[#bits+1] = b
if type(b) != 'string' and not (type(b) == 'table' and b.is_code)
b = repr(b)
if type(b) == 'string' if type(b) == 'string'
trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$") trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
if trailing_text if trailing_text
@ -132,6 +136,11 @@ class LuaCode extends Code
lua.is_value = true lua.is_value = true
return lua return lua
@Comment = (...)->
lua = LuaCode(...)
lua.is_comment = true
return lua
add_free_vars: (vars)=> add_free_vars: (vars)=>
return unless #vars > 0 return unless #vars > 0
seen = {[v]:true for v in *@free_vars} seen = {[v]:true for v in *@free_vars}

View File

@ -85,8 +85,6 @@ compile [local action %actions %body] to
end end
return lua return lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compile [action %actions %body] to compile [action %actions %body] to
lua> ".." lua> ".."
local lua = \(compile as: local action %actions %body) local lua = \(compile as: local action %actions %body)
@ -96,8 +94,6 @@ compile [action %actions %body] to
compile [action %action] to compile [action %action] to
Lua value "A\(%action.stub as lua id)" Lua value "A\(%action.stub as lua id)"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compile [parse %actions as %body] to compile [parse %actions as %body] to
lua> ".." lua> ".."
local replacements = {} local replacements = {}
@ -123,14 +119,8 @@ compile [parse %actions as %body] to
local ret = \(compile as: compile %actions to %new_body) local ret = \(compile as: compile %actions to %new_body)
return ret return ret
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compile [%tree as lua expr] to
lua> ".." Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree):as_expr()")):as_expr()"
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] to 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) elseif \%var.type == 'Action' then return "A"..string.as_lua_id(\%var.stub)
end end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compile [% is syntax tree] to compile [% is syntax tree] to
Lua value "AST.is_syntax_tree(\(% as lua expr))" Lua value "AST.is_syntax_tree(\(% as lua expr))"
@ -201,8 +189,6 @@ compile [quote %s] to
repr(\(%s as lua expr)) repr(\(%s as lua expr))
compile [type of %obj] to: Lua value "type(\(%obj as lua expr))" compile [type of %obj] to: Lua value "type(\(%obj as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compile [parse %text] to compile [parse %text] to
Lua value ".." Lua value ".."
nomsu:parse(NomsuCode("\("\(%text.source)")", \(%text as lua expr))) 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))) nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(%text as lua expr)))
compile [run %nomsu_code] to 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] action [run tree %tree, %tree as value]
lua> ".." lua> ".."
@ -236,8 +222,7 @@ compile [core version] to: Lua value "NOMSU_CORE_VERSION"
compile [lib version] to: Lua value "NOMSU_LIB_VERSION" compile [lib version] to: Lua value "NOMSU_LIB_VERSION"
compile [command line args] to: Lua value "arg" compile [command line args] to: Lua value "arg"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
compile [with local compile actions %body] to compile [with local compile actions %body] to
Lua ".." Lua ".."
do do
@ -245,8 +230,6 @@ compile [with local compile actions %body] to
\(%body as lua statements) \(%body as lua statements)
end end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
action [Nomsu version] action [Nomsu version]
use "lib/version.nom" use "lib/version.nom"
return "\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)" return "\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)"

View File

@ -355,6 +355,9 @@ do
source = nil source = nil
end end
source = source or (to_run.source or Source(to_run, 1, #to_run)) 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 if not files.read(source.filename) then
files.spoof(source.filename, to_run) files.spoof(source.filename, to_run)
end end

View File

@ -231,6 +231,7 @@ with NomsuCompiler
.run = (to_run, source=nil)=> .run = (to_run, source=nil)=>
source or= to_run.source or Source(to_run, 1, #to_run) 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) 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) 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 if tree == nil -- Happens if pattern matches, but there are no captures, e.g. an empty string