Added comments.
This commit is contained in:
parent
e8f2b4fdd2
commit
d218dcbd42
26
core.moon
26
core.moon
@ -39,17 +39,24 @@ g = PermissionNomic()
|
|||||||
g\defmacro [[lua %lua_code]], (vars,helpers,ftype)=>
|
g\defmacro [[lua %lua_code]], (vars,helpers,ftype)=>
|
||||||
with helpers
|
with helpers
|
||||||
lua_code = vars.lua_code.value
|
lua_code = vars.lua_code.value
|
||||||
escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
|
as_lua_code = (str)->
|
||||||
unescape = (s)-> s\gsub("\\(.)", ((c)-> escapes[c] or c))
|
switch str.type
|
||||||
|
when "String"
|
||||||
|
escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
|
||||||
|
unescaped = str.value\gsub("\\(.)", ((c)-> escapes[c] or c))
|
||||||
|
return unescaped
|
||||||
|
|
||||||
|
when "Longstring"
|
||||||
|
result = [line for line in str.value\gmatch("[ \t]*|([^\n]*)")]
|
||||||
|
return table.concat(result, "\n")
|
||||||
|
else
|
||||||
|
return str.value
|
||||||
|
|
||||||
switch lua_code.type
|
switch lua_code.type
|
||||||
when "List"
|
when "List"
|
||||||
-- TODO: handle subexpressions
|
-- TODO: handle subexpressions
|
||||||
.lua table.concat[unescape(i.value.value) for i in *lua_code.value]
|
.lua table.concat[as_lua_code(i.value) for i in *lua_code.value]
|
||||||
when "String"
|
else .lua(as_lua_code(lua_code))
|
||||||
.lua(unescape(lua_code.value))
|
|
||||||
when "Longstring"
|
|
||||||
.lua(lua_code.value)
|
|
||||||
else error("Unknown type: #{lua_code.type}")
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
g\def {"restrict %fn to %whitelist"}, (vars)=>
|
g\def {"restrict %fn to %whitelist"}, (vars)=>
|
||||||
@ -106,6 +113,9 @@ g\def [[printf %str]], (args)=>
|
|||||||
for s in *args.str do io.write(utils.repr(s))
|
for s in *args.str do io.write(utils.repr(s))
|
||||||
io.write("\n")
|
io.write("\n")
|
||||||
|
|
||||||
|
g\def [[concat %strs]], (vars)=>
|
||||||
|
return table.concat([utils.repr(s) for s in *vars.strs], "")
|
||||||
|
|
||||||
g\def [[quote %str]], (vars)=>
|
g\def [[quote %str]], (vars)=>
|
||||||
return utils.repr(vars.str, true)
|
return utils.repr(vars.str, true)
|
||||||
|
|
||||||
|
63
nomic.moon
63
nomic.moon
@ -5,15 +5,17 @@ utils = require 'utils'
|
|||||||
moon = require 'moon'
|
moon = require 'moon'
|
||||||
|
|
||||||
-- TODO:
|
-- TODO:
|
||||||
|
-- Comments
|
||||||
-- string interpolation
|
-- string interpolation
|
||||||
|
|
||||||
lpeg.setmaxstack 10000 -- whoa
|
lpeg.setmaxstack 10000 -- whoa
|
||||||
{:P,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
{:P,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
||||||
|
|
||||||
wordchar = P(1)-S(' \t\n\r%:;,.{}[]()"')
|
wordchar = P(1)-S(' \t\n\r%:;,.{}[]()"')
|
||||||
spaces = S(" \t")^1
|
comment = re.compile [[comment <- "(#" (comment / ((! "#)") .))* "#)"]]
|
||||||
|
whitespace = (S(" \t") + comment)^1
|
||||||
nl = P("\n")
|
nl = P("\n")
|
||||||
blank_line = spaces^-1 * nl
|
blank_line = whitespace^-1 * nl
|
||||||
|
|
||||||
get_line_indentation = (line)->
|
get_line_indentation = (line)->
|
||||||
indent_amounts = {[" "]:1, ["\t"]:4}
|
indent_amounts = {[" "]:1, ["\t"]:4}
|
||||||
@ -42,13 +44,12 @@ make_parser = (lingo, extra_definitions)->
|
|||||||
return end_pos
|
return end_pos
|
||||||
|
|
||||||
defs =
|
defs =
|
||||||
:wordchar, :nl, :spaces
|
:wordchar, :nl, ws:whitespace, :comment
|
||||||
ws: S(" \t")^1
|
|
||||||
eol: #nl + (P("")-P(1))
|
eol: #nl + (P("")-P(1))
|
||||||
word_boundary: S(" \t")^1 + B(P("..")) + B(S("\";)]")) + #S("\":([") + #P("..")
|
word_boundary: S(" \t")^1 + B(P("..")) + B(S("\";)]")) + #S("\":([") + #P("..") + #P("/*")
|
||||||
indent: #(nl * blank_line^0 * Cmt(spaces^-1, check_indent))
|
indent: #(nl * blank_line^0 * Cmt(whitespace^-1, check_indent))
|
||||||
dedent: #(nl * blank_line^0 * Cmt(spaces^-1, check_dedent))
|
dedent: #(nl * blank_line^0 * Cmt(whitespace^-1, check_dedent))
|
||||||
new_line: nl * blank_line^0 * Cmt(spaces^-1, check_nodent)
|
new_line: nl * blank_line^0 * Cmt(whitespace^-1, check_nodent)
|
||||||
error_handler: (src,pos,errors)->
|
error_handler: (src,pos,errors)->
|
||||||
line_no = 1
|
line_no = 1
|
||||||
for _ in src\sub(1,-#errors)\gmatch("\n") do line_no += 1
|
for _ in src\sub(1,-#errors)\gmatch("\n") do line_no += 1
|
||||||
@ -63,15 +64,14 @@ make_parser = (lingo, extra_definitions)->
|
|||||||
|
|
||||||
prev_line,err_line,next_line = src\match("([^\n]*)\n([^\n]*)\n([^\n]*)", start_of_prev_line+1)
|
prev_line,err_line,next_line = src\match("([^\n]*)\n([^\n]*)\n([^\n]*)", start_of_prev_line+1)
|
||||||
|
|
||||||
pointer = ("-")\rep(err_pos - start_of_err_line + 1) .. "^"
|
pointer = ("-")\rep(err_pos - start_of_err_line + 0) .. "^"
|
||||||
error("\nParse error on line #{line_no}:\n|#{prev_line}\n|#{err_line}\n#{pointer}\n|#{next_line}")
|
error("\nParse error on line #{line_no}:\n\n#{prev_line}\n#{err_line}\n#{pointer}\n#{next_line}\n")
|
||||||
|
|
||||||
if extra_definitions
|
if extra_definitions
|
||||||
for k,v in pairs(extra_definitions) do defs[k] = v
|
for k,v in pairs(extra_definitions) do defs[k] = v
|
||||||
|
|
||||||
setmetatable(defs, {
|
setmetatable(defs, {
|
||||||
__index: (t,key)->
|
__index: (t,key)->
|
||||||
--print("WORKING for #{key}")
|
|
||||||
fn = (src, value, errors)->
|
fn = (src, value, errors)->
|
||||||
token = {type: key, :src, :value, :errors}
|
token = {type: key, :src, :value, :errors}
|
||||||
return token
|
return token
|
||||||
@ -106,19 +106,13 @@ class Game
|
|||||||
invocations = {}
|
invocations = {}
|
||||||
local arg_names
|
local arg_names
|
||||||
for _text in *text
|
for _text in *text
|
||||||
name_bits = {}
|
invocation = _text\gsub("%%%S+","%%")
|
||||||
_arg_names = {}
|
_arg_names = [arg for arg in _text\gmatch("%%(%S+)")]
|
||||||
for chunk in _text\gmatch("%S+")
|
|
||||||
if chunk\sub(1,1) == "%"
|
|
||||||
table.insert name_bits, "%"
|
|
||||||
table.insert _arg_names, chunk\sub(2,-1)
|
|
||||||
else
|
|
||||||
table.insert name_bits, chunk
|
|
||||||
invocation = table.concat name_bits, " "
|
|
||||||
table.insert(invocations, invocation)
|
table.insert(invocations, invocation)
|
||||||
if arg_names and not utils.equivalent(utils.set(arg_names), utils.set(_arg_names))
|
if arg_names
|
||||||
error("Conflicting argument names #{utils.repr(arg_names)} and #{utils.repr(_arg_names)} for #{utils.repr(text)}")
|
if not utils.equivalent(utils.set(arg_names), utils.set(_arg_names))
|
||||||
arg_names = _arg_names
|
error("Conflicting argument names #{utils.repr(arg_names)} and #{utils.repr(_arg_names)} for #{utils.repr(text)}")
|
||||||
|
else arg_names = _arg_names
|
||||||
return invocations, arg_names
|
return invocations, arg_names
|
||||||
|
|
||||||
defmacro: (spec, fn)=>
|
defmacro: (spec, fn)=>
|
||||||
@ -131,12 +125,12 @@ class Game
|
|||||||
simplemacro: (spec, replacement)=>
|
simplemacro: (spec, replacement)=>
|
||||||
spec = spec\gsub("\r", "")
|
spec = spec\gsub("\r", "")
|
||||||
replacement = replacement\gsub("\r", "")
|
replacement = replacement\gsub("\r", "")
|
||||||
replace_grammar = [[
|
replace_grammar = [=[
|
||||||
stuff <- {~ (var / longstring / string / .)+ ~}
|
stuff <- {~ (var / longstring / string / .)+ ~}
|
||||||
var <- ("%" {%wordchar+}) -> replacer
|
var <- ("%" {%wordchar+}) -> replacer
|
||||||
string <- '"' (("\" .) / [^"])* '"'
|
string <- '"' (("\" .) / [^"])* '"'
|
||||||
longstring <- ('"..' %indent %nl {(!%dedent .)*} %new_line '.."')
|
longstring <- ('".."' %ws? %indent {(%new_line "|" [^%nl]*)+} %dedent (%new_line '..')?)
|
||||||
]]
|
]=]
|
||||||
fn = (vars, helpers, ftype)=>
|
fn = (vars, helpers, ftype)=>
|
||||||
replacer = (varname)->
|
replacer = (varname)->
|
||||||
ret = vars[varname].src
|
ret = vars[varname].src
|
||||||
@ -153,12 +147,10 @@ class Game
|
|||||||
|
|
||||||
run: (text)=>
|
run: (text)=>
|
||||||
if @debug
|
if @debug
|
||||||
print("RUNNING TEXT:\n")
|
print "RUNNING TEXT:\n#{text}"
|
||||||
print(text)
|
|
||||||
code = self\compile(text)
|
code = self\compile(text)
|
||||||
if @debug
|
if @debug
|
||||||
print("\nGENERATED LUA CODE:")
|
print "\nGENERATED LUA CODE:\n#{code}"
|
||||||
print(code)
|
|
||||||
lua_thunk, err = loadstring(code)
|
lua_thunk, err = loadstring(code)
|
||||||
if not lua_thunk
|
if not lua_thunk
|
||||||
error("Failed to compile generated code:\n#{code}\n\n#{err}")
|
error("Failed to compile generated code:\n#{code}\n\n#{err}")
|
||||||
@ -209,12 +201,12 @@ class Game
|
|||||||
expression <- ({ (longstring / string / number / variable / list / thunk / subexpression) }) -> Expression
|
expression <- ({ (longstring / string / number / variable / list / thunk / subexpression) }) -> Expression
|
||||||
|
|
||||||
string <- ({ (!('"..' %ws? %nl)) '"' {(("\" .) / [^"])*} '"' }) -> String
|
string <- ({ (!('"..' %ws? %nl)) '"' {(("\" .) / [^"])*} '"' }) -> String
|
||||||
longstring <- ({ '"..' %ws? %indent %nl {(!%dedent .)* (%nl %ws? %eol)*} ((%new_line '.."') / errors) }) -> Longstring
|
longstring <- ({ '".."' %ws? %indent {(%new_line "|" [^%nl]*)+} ((%dedent (%new_line '..')?) / errors) }) -> Longstring
|
||||||
number <- ({ {'-'? [0-9]+ ("." [0-9]+)?} }) -> Number
|
number <- ({ {'-'? [0-9]+ ("." [0-9]+)?} }) -> Number
|
||||||
variable <- ({ ("%" {%wordchar+}) }) -> Var
|
variable <- ({ ("%" {%wordchar+}) }) -> Var
|
||||||
|
|
||||||
subexpression <-
|
subexpression <-
|
||||||
("(" %ws? (functioncall / expression) %ws? ")")
|
(!%comment "(" %ws? (functioncall / expression) %ws? ")")
|
||||||
/ ("(..)" %ws? %indent %new_line ((({ {| indented_fn_bits |} }) -> FunctionCall) / expression) %dedent (%new_line "..")?)
|
/ ("(..)" %ws? %indent %new_line ((({ {| indented_fn_bits |} }) -> FunctionCall) / expression) %dedent (%new_line "..")?)
|
||||||
|
|
||||||
list <- ({ {|
|
list <- ({ {|
|
||||||
@ -337,12 +329,7 @@ class Game
|
|||||||
lua utils.repr(unescaped, true)
|
lua utils.repr(unescaped, true)
|
||||||
|
|
||||||
when "Longstring"
|
when "Longstring"
|
||||||
first_nonblank_line = tree.value\match("[^\n]+")
|
result = [line for line in tree.value\gmatch("[ \t]*|([^\n]*)")]
|
||||||
indent = first_nonblank_line\match("[ \t]*")
|
|
||||||
result = {}
|
|
||||||
for line in (tree.value.."\n")\gmatch("(.-)\n")
|
|
||||||
line = line\gsub("^"..indent, "", 1)
|
|
||||||
table.insert result, line
|
|
||||||
lua utils.repr(table.concat(result, "\n"), true)
|
lua utils.repr(table.concat(result, "\n"), true)
|
||||||
|
|
||||||
when "Number"
|
when "Number"
|
||||||
|
Loading…
Reference in New Issue
Block a user