Removing end-of-line ":" and "(..)" for blocks (they just use
indentation now).
This commit is contained in:
parent
50a092e4b5
commit
5d59d510cd
@ -9,18 +9,18 @@ use "core/operators.nom"
|
||||
# List/dict functions:
|
||||
|
||||
# Indexing
|
||||
immediately:
|
||||
immediately
|
||||
compile [..]
|
||||
%index st to last in %list, %index nd to last in %list, %index rd to last in %list
|
||||
%index th to last in %list
|
||||
..to: Lua value "utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))"
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
parse [first in %list, first %list] as: 1 st in %list
|
||||
parse [last in %list, last %list] as: 1 st to last in %list
|
||||
|
||||
# Membership testing
|
||||
immediately:
|
||||
immediately
|
||||
action [%item is in %list, %list contains %item, %list has %item]
|
||||
for %key = %value in %list
|
||||
if (%key is %item): return (yes)
|
||||
@ -30,12 +30,12 @@ immediately:
|
||||
%item isn't in %list, %item is not in %list
|
||||
%list doesn't contain %item, %list does not contain %item
|
||||
%list doesn't have %item, %list does not have %item
|
||||
..:
|
||||
..
|
||||
for %key = %value in %list
|
||||
if (%key is %item): return (no)
|
||||
return (yes)
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
# Note: it's important to have the space after "[" to prevent confusion if %index is a string
|
||||
compile [%list has key %index, %list has index %index] to
|
||||
Lua value ".."
|
||||
@ -47,21 +47,21 @@ immediately:
|
||||
%list doesn't have index %index, %list does not have index %index
|
||||
..to: Lua value "((\(%list as lua expr))[ \(%index as lua expr)] == nil)"
|
||||
|
||||
compile [number of keys in %list] to:
|
||||
compile [number of keys in %list] to
|
||||
Lua value "utils.size(\(%list as lua expr))"
|
||||
|
||||
compile [append %item to %list, add %item to %list, to %list add %item, to %list append %item] to:
|
||||
compile [append %item to %list, add %item to %list, to %list add %item, to %list append %item] to
|
||||
Lua "table.insert(\(%list as lua expr), \(%item as lua expr))"
|
||||
|
||||
compile [pop from %list, remove last from %list] to:
|
||||
compile [pop from %list, remove last from %list] to
|
||||
Lua "table.remove(\(%list as lua expr))"
|
||||
|
||||
compile [remove index %index from %list] to:
|
||||
compile [remove index %index from %list] to
|
||||
Lua "table.remove(\(%list as lua expr), \(%index as lua expr))"
|
||||
|
||||
# List Comprehension
|
||||
immediately:
|
||||
compile [%expression for %item in %iterable] to:
|
||||
immediately
|
||||
compile [%expression for %item in %iterable] to
|
||||
assume (%item.type is "Var") or barf ".."
|
||||
List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item.type)
|
||||
return
|
||||
@ -78,7 +78,7 @@ immediately:
|
||||
compile [..]
|
||||
%expression for %index from %start to %stop via %step
|
||||
%expression for %index from %start to %stop by %step
|
||||
..to:
|
||||
..to
|
||||
assume (%index.type is "Var") or barf ".."
|
||||
List comprehension has the wrong type for the loop variable. Expected Var, but got: \(%index.type)
|
||||
return
|
||||
@ -98,7 +98,7 @@ immediately:
|
||||
..as: %expression for % from %start to %stop via %step
|
||||
parse [%expression for all %start to %stop] as: %expression for all %start to %stop via 1
|
||||
|
||||
compile [%expression for %key = %value in %iterable] to:
|
||||
compile [%expression for %key = %value in %iterable] to
|
||||
assume (%key.type is "Var") or barf ".."
|
||||
List comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%key.type)
|
||||
assume (%value.type is "Var") or barf ".."
|
||||
@ -114,8 +114,8 @@ immediately:
|
||||
end)()
|
||||
|
||||
# Dict comprehensions
|
||||
immediately:
|
||||
compile [%key = %value for %item in %iterable] to:
|
||||
immediately
|
||||
compile [%key = %value for %item in %iterable] to
|
||||
assume (%item.type is "Var") or barf ".."
|
||||
Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \(%item.type)
|
||||
# Note: it's important to have the space after "[" to prevent confusion if %key is a string
|
||||
@ -130,7 +130,7 @@ immediately:
|
||||
end)()
|
||||
parse [%key = %value for all %iterable] as: %key = %value for % in %iterable
|
||||
|
||||
compile [%key = %value for %src_key = %src_value in %iterable] to:
|
||||
compile [%key = %value for %src_key = %src_value in %iterable] to
|
||||
assume (%src_key.type is "Var") or barf ".."
|
||||
Dict comprehension has the wrong type for the key loop variable. Expected Var, but got: \(%src_key.type)
|
||||
assume (%src_value.type is "Var") or barf ".."
|
||||
@ -146,11 +146,11 @@ immediately:
|
||||
return comprehension;
|
||||
end)()
|
||||
|
||||
immediately:
|
||||
action [%lists flattened]:
|
||||
immediately
|
||||
action [%lists flattened]
|
||||
%flat <- []
|
||||
for %list in %lists:
|
||||
for %item in %list:
|
||||
for %list in %lists
|
||||
for %item in %list
|
||||
add %item to %flat
|
||||
return %flat
|
||||
|
||||
@ -158,8 +158,8 @@ immediately:
|
||||
parse [keys in %dict] as: %k for %k = %v in %dict
|
||||
parse [values in %dict] as: %v for %k = %v in %dict
|
||||
|
||||
# Sorting:
|
||||
immediately:
|
||||
# Sorting
|
||||
immediately
|
||||
compile [sort %items] to: Lua "table.sort(\(%items as lua expr));"
|
||||
compile [sort %items by %key_expr] to
|
||||
Lua ".."
|
||||
@ -167,26 +167,26 @@ immediately:
|
||||
return \(%key_expr as lua expr);
|
||||
end);
|
||||
|
||||
immediately:
|
||||
action [%items sorted, sorted %items]:
|
||||
immediately
|
||||
action [%items sorted, sorted %items]
|
||||
%copy <- (% for all %items)
|
||||
sort %copy
|
||||
return %copy
|
||||
action [%items sorted by %key]:
|
||||
action [%items sorted by %key]
|
||||
%copy <- (% for all %items)
|
||||
sort %copy by %key
|
||||
return %copy
|
||||
|
||||
action [unique %items]:
|
||||
action [unique %items]
|
||||
%unique <- []
|
||||
%seen <- {}
|
||||
for all %items:
|
||||
for all %items
|
||||
unless: % in %seen
|
||||
add % to %unique
|
||||
(% in %seen) <- (yes)
|
||||
return %unique
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
# Metatable stuff
|
||||
compile [set %dict's metatable to %metatable] to
|
||||
Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
|
||||
|
@ -7,19 +7,19 @@ use "core/text.nom"
|
||||
use "core/operators.nom"
|
||||
|
||||
# No-Op
|
||||
immediately:
|
||||
immediately
|
||||
compile [do nothing] to: Lua ""
|
||||
|
||||
# Conditionals
|
||||
immediately:
|
||||
compile [if %condition %if_body] to:
|
||||
immediately
|
||||
compile [if %condition %if_body] to
|
||||
Lua ".."
|
||||
if \(%condition as lua expr) then
|
||||
\(%if_body as lua statements)
|
||||
end
|
||||
parse [unless %condition %unless_body] as: if (not %condition) %unless_body
|
||||
|
||||
compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to:
|
||||
compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to
|
||||
Lua ".."
|
||||
if \(%condition as lua expr) then
|
||||
\(%if_body as lua statements)
|
||||
@ -36,18 +36,18 @@ immediately
|
||||
%when_true_expr if %condition otherwise %when_false_expr
|
||||
%when_false_expr unless %condition else %when_true_expr
|
||||
%when_false_expr unless %condition then %when_true_expr
|
||||
..to:
|
||||
..to
|
||||
#.. If %when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic
|
||||
equivalent of a conditional expression: (cond and if_true or if_false)
|
||||
if: %when_true_expr.type in {Text:yes, List:yes, Dict:yes, Number:yes}
|
||||
return:
|
||||
return
|
||||
Lua value ".."
|
||||
(\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))
|
||||
..else:
|
||||
..else
|
||||
#.. Otherwise, need to do an anonymous inline function (yuck, too bad lua
|
||||
doesn't have a proper ternary operator!)
|
||||
To see why this is necessary consider: (random()<.5 and false or 99)
|
||||
return:
|
||||
return
|
||||
Lua value ".."
|
||||
(function()
|
||||
if \(%condition as lua expr) then
|
||||
@ -58,20 +58,20 @@ immediately
|
||||
end)()
|
||||
|
||||
# GOTOs
|
||||
immediately:
|
||||
immediately
|
||||
compile [=== %label ===, --- %label ---, *** %label ***] to
|
||||
Lua "::label_\(%label as lua identifier)::;"
|
||||
compile [go to %label] to
|
||||
Lua "goto label_\(%label as lua identifier);"
|
||||
|
||||
# Basic loop control
|
||||
immediately:
|
||||
immediately
|
||||
compile [do next] to: Lua "continue;"
|
||||
compile [stop] to: Lua "break;"
|
||||
|
||||
# Helper function
|
||||
immediately:
|
||||
compile [if %tree has subtree %subtree where %condition %body] to:
|
||||
immediately
|
||||
compile [if %tree has subtree %subtree where %condition %body] to
|
||||
Lua ".."
|
||||
for \(%subtree as lua expr) in coroutine.wrap(function() nomsu:walk_tree(\(%tree as lua expr)) end) do
|
||||
if Types.is_node(\(%subtree as lua expr)) then
|
||||
@ -83,7 +83,7 @@ immediately:
|
||||
end
|
||||
|
||||
# While loops
|
||||
immediately:
|
||||
immediately
|
||||
compile [do next repeat] to: Lua "goto continue_repeat;"
|
||||
compile [stop repeating] to: Lua "goto stop_repeat;"
|
||||
compile [repeat while %condition %body] to
|
||||
@ -91,14 +91,14 @@ immediately:
|
||||
Lua ".."
|
||||
while \(%condition as lua expr) do
|
||||
\(%body as lua statements)
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
||||
..:
|
||||
..
|
||||
to %lua write "\n ::continue_repeat::;"
|
||||
to %lua write "\nend --while-loop"
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
||||
..:
|
||||
..
|
||||
%lua <-
|
||||
Lua ".."
|
||||
do -- scope of "stop repeating" label
|
||||
@ -111,7 +111,7 @@ immediately:
|
||||
|
||||
compile [..]
|
||||
repeat %n times %body
|
||||
..to:
|
||||
..to
|
||||
%lua <-
|
||||
Lua ".."
|
||||
for i=1,\(%n as lua expr) do
|
||||
@ -120,10 +120,10 @@ immediately:
|
||||
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
||||
..: to %lua write "\n ::continue_repeat::;"
|
||||
to %lua write "\nend --numeric for-loop"
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
||||
..:
|
||||
%lua <-:
|
||||
..
|
||||
%lua <-
|
||||
Lua ".."
|
||||
do -- scope of "stop repeating" label
|
||||
\%lua
|
||||
@ -131,37 +131,37 @@ immediately:
|
||||
end -- end of "stop repeating" label scope
|
||||
return %lua
|
||||
|
||||
# For loop control flow:
|
||||
immediately:
|
||||
# For loop control flow
|
||||
immediately
|
||||
compile [stop %var] to
|
||||
Lua "goto stop_\(%var as lua identifier);"
|
||||
compile [do next %var] to
|
||||
Lua "goto continue_\(%var as lua identifier);"
|
||||
|
||||
# Numeric range for loops
|
||||
immediately:
|
||||
immediately
|
||||
compile [..]
|
||||
for %var from %start to %stop by %step %body
|
||||
for %var from %start to %stop via %step %body
|
||||
..to:
|
||||
..to
|
||||
# This uses Lua's approach of only allowing loop-scoped variables in a loop
|
||||
assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
|
||||
%lua <-
|
||||
Lua ".."
|
||||
for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do
|
||||
\(%body as lua statements)
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "do next %") and
|
||||
%.value.3.value is %var.value
|
||||
..: to %lua write "\n ::continue_\(%var as lua identifier)::;"
|
||||
to %lua write "\nend --numeric for-loop"
|
||||
|
||||
if %body has subtree % where:
|
||||
(%.type = "Action") and:
|
||||
((%'s stub) is "stop %") and:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "stop %") and
|
||||
%.value.2.value is %var.value
|
||||
..:
|
||||
..
|
||||
%lua <-
|
||||
Lua ".."
|
||||
do -- scope for stopping for-loop
|
||||
@ -171,7 +171,7 @@ immediately:
|
||||
|
||||
return %lua
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
parse [for %var from %start to %stop %body] as: for %var from %start to %stop via 1 %body
|
||||
parse [..]
|
||||
for all %start to %stop by %step %body
|
||||
@ -180,25 +180,25 @@ immediately:
|
||||
parse [for all %start to %stop %body] as: for all %start to %stop via 1 %body
|
||||
|
||||
# For-each loop (lua's "ipairs()")
|
||||
immediately:
|
||||
compile [for %var in %iterable %body] to:
|
||||
immediately
|
||||
compile [for %var in %iterable %body] to
|
||||
# This uses Lua's approach of only allowing loop-scoped variables in a loop
|
||||
assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
|
||||
%lua <-
|
||||
Lua ".."
|
||||
for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do
|
||||
\(%body as lua statements)
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "do next %") and
|
||||
%.value.3.value is %var.value
|
||||
..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::;")
|
||||
to %lua write "\nend --foreach-loop"
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "stop %") and
|
||||
%.value.2.value is %var.value
|
||||
..:
|
||||
..
|
||||
%lua <-
|
||||
Lua ".."
|
||||
do -- scope for stopping for-loop
|
||||
@ -210,8 +210,8 @@ immediately:
|
||||
parse [for all %iterable %body] as: for % in %iterable %body
|
||||
|
||||
# Dict iteration (lua's "pairs()")
|
||||
immediately:
|
||||
compile [for %key = %value in %iterable %body] to:
|
||||
immediately
|
||||
compile [for %key = %value in %iterable %body] to
|
||||
# This uses Lua's approach of only allowing loop-scoped variables in a loop
|
||||
assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)"
|
||||
assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)"
|
||||
@ -219,13 +219,13 @@ immediately:
|
||||
Lua ".."
|
||||
for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do
|
||||
\(%body as lua statements)
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "do next %") and
|
||||
%.value.3.value is %key.value
|
||||
..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::;")
|
||||
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "do next %") and
|
||||
%.value.3.value is %value.value
|
||||
@ -233,13 +233,13 @@ immediately:
|
||||
to %lua write "\nend --foreach-loop"
|
||||
|
||||
%stop_labels <- (Lua "")
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "stop %") and
|
||||
%.value.2.value is %key.value
|
||||
..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;"
|
||||
|
||||
if %body has subtree % where:
|
||||
if %body has subtree % where
|
||||
(%.type = "Action") and
|
||||
((%'s stub) is "stop %") and
|
||||
%.value.2.value is %value.value
|
||||
@ -255,13 +255,13 @@ immediately:
|
||||
return %lua
|
||||
|
||||
# Switch statement/multi-branch if
|
||||
immediately:
|
||||
compile [when %body] to:
|
||||
immediately
|
||||
compile [when %body] to
|
||||
%code <- (Lua "")
|
||||
%fallthroughs <- []
|
||||
%is_first <- (yes)
|
||||
%seen_else <- (no)
|
||||
for %func_call in %body.value:
|
||||
for %func_call in %body.value
|
||||
assume (%func_call.type is "Action") or barf ".."
|
||||
Invalid format for 'when' statement. Only '*' blocks are allowed.
|
||||
%tokens <- %func_call.value
|
||||
@ -269,7 +269,7 @@ immediately:
|
||||
%star: %tokens.1
|
||||
%condition: %tokens.2
|
||||
%action: %tokens.3
|
||||
..:
|
||||
..
|
||||
assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
|
||||
Invalid format for 'when' statement. Lines must begin with '*'
|
||||
assume %condition or barf ".."
|
||||
@ -283,11 +283,11 @@ immediately:
|
||||
to %code write "\nelse\n "
|
||||
to %code write: %action as lua statements
|
||||
%seen_else <- (yes)
|
||||
..else:
|
||||
..else
|
||||
assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block"
|
||||
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
|
||||
to %code write "\("if" if %is_first else "\nelseif") "
|
||||
for %i=%condition in %fallthroughs:
|
||||
for %i=%condition in %fallthroughs
|
||||
if (%i > 1): to %code write " or "
|
||||
to %code write %condition
|
||||
to %code write " then\n "
|
||||
@ -302,17 +302,17 @@ immediately:
|
||||
return %code
|
||||
|
||||
# Switch statement
|
||||
immediately:
|
||||
compile [when %branch_value = ? %body, when %branch_value is ? %body] to:
|
||||
immediately
|
||||
compile [when %branch_value = ? %body, when %branch_value is ? %body] to
|
||||
%code <- (Lua "")
|
||||
%fallthroughs <- []
|
||||
%is_first <- (yes)
|
||||
%seen_else <- (no)
|
||||
for %func_call in %body.value:
|
||||
for %func_call in %body.value
|
||||
assume (%func_call.type is "Action") or barf ".."
|
||||
Invalid format for 'when' statement. Only '*' blocks are allowed.
|
||||
%tokens <- %func_call.value
|
||||
with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}:
|
||||
with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}
|
||||
assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
|
||||
Invalid format for 'when' statement. Lines must begin with '*'
|
||||
assume %condition or barf ".."
|
||||
@ -325,7 +325,7 @@ immediately:
|
||||
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
|
||||
to %code write "\nelse\n "
|
||||
to %code write: %action as lua statements
|
||||
..else:
|
||||
..else
|
||||
assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block"
|
||||
to %code write "\("if" if %is_first else "\nelseif") "
|
||||
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
|
||||
@ -334,7 +334,7 @@ immediately:
|
||||
to %code write " or "
|
||||
if: (%.type is "Text") or (%.type is "Number")
|
||||
to %code write "branch_value == \%"
|
||||
..else:
|
||||
..else
|
||||
to %code write "utils.equivalent(branch_value, \%)"
|
||||
to %code write "then\n "
|
||||
to %code write (%action as lua statements)
|
||||
@ -354,11 +354,11 @@ immediately:
|
||||
return %code
|
||||
|
||||
# Try/except
|
||||
immediately:
|
||||
immediately
|
||||
compile [..]
|
||||
try %action and if it succeeds %success or if it barfs %fallback
|
||||
try %action and if it barfs %fallback or if it succeeds %success
|
||||
..to:
|
||||
..to
|
||||
Lua ".."
|
||||
do
|
||||
local fell_through = false;
|
||||
@ -375,24 +375,24 @@ immediately:
|
||||
return ret;
|
||||
end
|
||||
end
|
||||
parse [try %action] as:
|
||||
parse [try %action] as
|
||||
try %action and if it succeeds: do nothing
|
||||
..or if it barfs: do nothing
|
||||
parse [try %action and if it barfs %fallback] as:
|
||||
parse [try %action and if it barfs %fallback] as
|
||||
try %action and if it succeeds: do nothing
|
||||
..or if it barfs %fallback
|
||||
parse [try %action and if it succeeds %success] as:
|
||||
parse [try %action and if it succeeds %success] as
|
||||
try %action and if it succeeds %success or if it barfs: do nothing
|
||||
|
||||
# Do/finally:
|
||||
immediately:
|
||||
compile [do %action] to:
|
||||
# Do/finally
|
||||
immediately
|
||||
compile [do %action] to
|
||||
Lua ".."
|
||||
do
|
||||
\(%action as lua statements)
|
||||
end --do
|
||||
|
||||
compile [do %action then always %final_action] to:
|
||||
compile [do %action then always %final_action] to
|
||||
Lua ".."
|
||||
do
|
||||
local fell_through = false;
|
||||
|
@ -34,37 +34,37 @@ compile [log % base %base, log_%base %, log base %base %] to: Lua value "math.lo
|
||||
compile [floor %] to: Lua value "math.floor(\(% as lua expr))"
|
||||
compile [ceiling %, ceil %] to: Lua value "math.ceil(\(% as lua expr))"
|
||||
compile [round %, % rounded] to: Lua value "math.floor(\(% as lua expr) + .5)"
|
||||
action [%n to the nearest %rounder]:
|
||||
action [%n to the nearest %rounder]
|
||||
=lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)"
|
||||
|
||||
# Any/all/none
|
||||
compile [all of %items, all %items] to:
|
||||
compile [all of %items, all %items] to
|
||||
unless: (%items' "type") is "List"
|
||||
return: Lua value "utils.all(\(%items as lua expr))"
|
||||
%clauses <- []
|
||||
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
|
||||
return: Lua value "(\(%clauses joined with " and "))"
|
||||
parse [not all of %items, not all %items] as: not (all of %items)
|
||||
compile [any of %items, any %items] to:
|
||||
compile [any of %items, any %items] to
|
||||
unless: (%items' "type") is "List"
|
||||
return: Lua value "utils.any(\(%items as lua expr))"
|
||||
%clauses <- []
|
||||
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
|
||||
return: Lua value "(\(%clauses joined with " or "))"
|
||||
parse [none of %items, none %items] as: not (any of %items)
|
||||
compile [sum of %items, sum %items] to:
|
||||
compile [sum of %items, sum %items] to
|
||||
unless: (%items' "type") is "List"
|
||||
return: Lua value "utils.sum(\(%items as lua expr))"
|
||||
%clauses <- []
|
||||
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
|
||||
return: Lua value "(\(%clauses joined with " + "))"
|
||||
compile [product of %items, product %items] to:
|
||||
compile [product of %items, product %items] to
|
||||
unless: (%items' "type") is "List"
|
||||
return: Lua value "utils.product(\(%items as lua expr))"
|
||||
%clauses <- []
|
||||
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
|
||||
return: Lua value "(\(%clauses joined with " * "))"
|
||||
action [avg of %items, average of %items]:
|
||||
action [avg of %items, average of %items]
|
||||
=lua "(utils.sum(\%items)/#\%items)"
|
||||
compile [min of %items, smallest of %items, lowest of %items] to
|
||||
Lua value "utils.min(\(%items as lua expr))"
|
||||
@ -82,7 +82,7 @@ compile [max of %items by %value_expr] to
|
||||
end)
|
||||
|
||||
# Random functions
|
||||
action [seed random with %]:
|
||||
action [seed random with %]
|
||||
lua> ".."
|
||||
math.randomseed(\%);
|
||||
for i=1,20 do math.random(); end
|
||||
@ -91,5 +91,5 @@ compile [random number, random, rand] to: Lua value "math.random()"
|
||||
compile [random int %n, random integer %n, randint %n] to: Lua value "math.random(\(%n as lua expr))"
|
||||
compile [random from %low to %high, random number from %low to %high, rand %low %high] to
|
||||
"math.random(\(%low as lua expr), \(%high as lua expr))"
|
||||
action [random choice from %elements, random choice %elements, random %elements]:
|
||||
action [random choice from %elements, random choice %elements, random %elements]
|
||||
=lua "\%elements[math.random(#\%elements)]"
|
||||
|
@ -3,7 +3,7 @@
|
||||
functions to make that easier.
|
||||
|
||||
# Compile-time action to make compile-time actions:
|
||||
immediately:
|
||||
immediately
|
||||
lua> ".."
|
||||
nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(tree, \%actions, \%lua)
|
||||
local lua = Lua(tree.source, "nomsu:define_compile_action(");
|
||||
@ -31,15 +31,15 @@ immediately:
|
||||
end
|
||||
local body_lua = \%lua:as_lua(nomsu);
|
||||
body_lua:convert_to_statements("return ");
|
||||
body_lua:remove_free_vars(args);
|
||||
body_lua:remove_free_vars(unpack(args));
|
||||
body_lua:declare_locals();
|
||||
lua:append(")\\n ", body_lua, "\\nend);");
|
||||
return lua;
|
||||
end);
|
||||
|
||||
# Compile-time action to make actions
|
||||
immediately:
|
||||
compile [action %actions %body] to:
|
||||
immediately
|
||||
compile [action %actions %body] to
|
||||
lua> ".."
|
||||
local lua = Lua(tree.source, "nomsu:define_action(");
|
||||
local stubs = {};
|
||||
@ -63,14 +63,14 @@ immediately:
|
||||
end
|
||||
local body_lua = \%body:as_lua(nomsu);
|
||||
body_lua:convert_to_statements("return ");
|
||||
body_lua:remove_free_vars(args);
|
||||
body_lua:remove_free_vars(unpack(args));
|
||||
body_lua:declare_locals();
|
||||
lua:append(")\\n ", body_lua, "\\nend);")
|
||||
return lua;
|
||||
|
||||
# Macro to make nomsu macros:
|
||||
immediately:
|
||||
compile [parse %shorthand as %longhand] to:
|
||||
# Macro to make nomsu macros
|
||||
immediately
|
||||
compile [parse %shorthand as %longhand] to
|
||||
lua> ".."
|
||||
local lua = Lua(tree.source, "nomsu:define_compile_action(");
|
||||
local stubs = {};
|
||||
@ -104,7 +104,7 @@ immediately:
|
||||
]]);
|
||||
return lua;
|
||||
|
||||
action [remove action %stub]:
|
||||
action [remove action %stub]
|
||||
lua> ".."
|
||||
local fn = ACTIONS[\%stub];
|
||||
local metadata = nomsu.action_metadata[fn];
|
||||
@ -114,11 +114,11 @@ action [remove action %stub]:
|
||||
end
|
||||
ACTIONS[\%stub] = nil;
|
||||
|
||||
immediately:
|
||||
action [%tree as lua]:
|
||||
immediately
|
||||
action [%tree as lua]
|
||||
=lua "\%tree:as_lua(nomsu)"
|
||||
|
||||
action [%tree as lua expr]:
|
||||
action [%tree as lua expr]
|
||||
lua> ".."
|
||||
local lua = \%tree:as_lua(nomsu);
|
||||
if not lua.is_value then
|
||||
@ -126,7 +126,7 @@ immediately:
|
||||
end
|
||||
return lua;
|
||||
|
||||
action [%tree as lua statements]:
|
||||
action [%tree as lua statements]
|
||||
lua> ".."
|
||||
local lua = \%tree:as_lua(nomsu);
|
||||
lua:convert_to_statements();
|
||||
@ -138,37 +138,37 @@ immediately:
|
||||
compile [declare locals %locals in %tree] to
|
||||
Lua "\(%tree as lua expr):declare_locals(\(%locals as lua expr));"
|
||||
|
||||
compile [remove free vars %vars from %tree] to:
|
||||
compile [remove free vars %vars from %tree] to
|
||||
Lua "\(%tree as lua expr):remove_free_vars(unpack(\(%vars as lua expr)));"
|
||||
|
||||
action [%tree as value]:
|
||||
action [%tree as value]
|
||||
=lua "nomsu:tree_to_value(\%tree)"
|
||||
|
||||
|
||||
action [%tree's stub]:
|
||||
action [%tree's stub]
|
||||
=lua "nomsu:tree_to_stub(\%tree)"
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
parse [%var <-write %code] as: lua> "\%var:append(\%code);"
|
||||
parse [to %var write %code] as: lua> "\%var:append(\%code);"
|
||||
|
||||
immediately:
|
||||
action [%tree's source code, %tree' source code]:
|
||||
immediately
|
||||
action [%tree's source code, %tree' source code]
|
||||
=lua "\%tree.source:get_text()"
|
||||
|
||||
compile [repr %obj] to: Lua value "repr(\(%obj as lua expr))"
|
||||
compile [%obj as text] to: Lua value "tostring(\(%obj as lua expr))"
|
||||
compile [type of %obj] to: Lua value "type(\(%obj as lua expr))"
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
compile [nomsu] to: Lua value "nomsu"
|
||||
compile [%var as lua identifier] to: Lua value "nomsu:var_to_lua_identifier(\(%var as lua expr))"
|
||||
|
||||
action [action %names metadata]:
|
||||
action [action %names metadata]
|
||||
=lua "nomsu.action_metadata[ACTIONS[\%names]]"
|
||||
|
||||
# Get the source code for a function
|
||||
action [help %action]:
|
||||
action [help %action]
|
||||
lua> ".."
|
||||
local metadata = \(action %action metadata);
|
||||
if not metadata then
|
||||
@ -178,20 +178,20 @@ action [help %action]:
|
||||
end
|
||||
|
||||
# Compiler tools
|
||||
immediately:
|
||||
compile [run %code] to:
|
||||
immediately
|
||||
compile [run %code] to
|
||||
Lua value "nomsu:run(Nomsu(\(=lua "tostring(tree.source)"), \(%code as lua expr)))"
|
||||
parse [enable debugging] as: lua> "nomsu.debug = true;"
|
||||
parse [disable debugging] as: lua> "nomsu.debug = false;"
|
||||
|
||||
immediately:
|
||||
compile [show lua %block] to:
|
||||
immediately
|
||||
compile [show lua %block] to
|
||||
lua> ".."
|
||||
local \%lua = \%block:as_lua(nomsu);
|
||||
return Lua(\%block.source, "print(", repr(tostring(\%lua)), ");");
|
||||
|
||||
immediately:
|
||||
compile [say %message] to:
|
||||
immediately
|
||||
compile [say %message] to
|
||||
lua> ".."
|
||||
if \%message.type == "Text" then
|
||||
return Lua(tree.source, "print(", \(%message as lua expr), ");");
|
||||
@ -199,42 +199,42 @@ immediately:
|
||||
return Lua(tree.source, "print(stringify(", \(%message as lua expr), "));");
|
||||
end
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
compile [source] to: Lua value (=lua "tree.source") "tree.source"
|
||||
|
||||
#..
|
||||
immediately:
|
||||
immediately
|
||||
action [Lua %]: Lua (=lua "tree.source") %
|
||||
action [Lua value %]: Lua value (=lua "tree.source") %
|
||||
|
||||
# Return
|
||||
immediately:
|
||||
immediately
|
||||
#.. Return statement is wrapped in a do..end block because Lua is unhappy if you
|
||||
put code after a return statement, unless you wrap it in a block.
|
||||
compile [return] to: Lua "do return; end"
|
||||
compile [return %return_value] to: Lua "do return \(%return_value as lua expr); end"
|
||||
|
||||
# Error functions
|
||||
immediately:
|
||||
immediately
|
||||
compile [barf] to: Lua "error(nil, 0);"
|
||||
compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);"
|
||||
compile [assume %condition] to:
|
||||
compile [assume %condition] to
|
||||
lua> "local \%assumption = 'Assumption failed: '..tostring(\%condition.source:get_text());"
|
||||
return:
|
||||
return
|
||||
Lua ".."
|
||||
if not \(%condition as lua expr) then
|
||||
error(\(repr %assumption), 0);
|
||||
end
|
||||
|
||||
# TODO: factor this out and replace with "unless %condition: barf %message"
|
||||
compile [assume %condition or barf %message] to:
|
||||
compile [assume %condition or barf %message] to
|
||||
Lua ".."
|
||||
if not \(%condition as lua expr) then
|
||||
error(\(%message as lua expr), 0);
|
||||
end
|
||||
|
||||
# Literals
|
||||
immediately:
|
||||
immediately
|
||||
compile [yes] to: Lua value "true"
|
||||
compile [no] to: Lua value "false"
|
||||
compile [nothing, nil, null] to: Lua value "nil"
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
use "core/metaprogramming.nom"
|
||||
|
||||
# Indexing:
|
||||
immediately:
|
||||
# Indexing
|
||||
immediately
|
||||
#.. NOTE!!! It's critical that there are spaces around %key if it's a string,
|
||||
otherwise, Lua will get confused and interpret %obj[[[foo]]] as %obj("[foo]")
|
||||
instead of %obj[ "foo" ].
|
||||
@ -17,13 +17,13 @@ immediately:
|
||||
..as: %obj.%key
|
||||
|
||||
# Comparison Operators
|
||||
immediately:
|
||||
immediately
|
||||
compile [%x < %y] to: Lua value "(\(%x as lua expr) < \(%y as lua expr))"
|
||||
compile [%x > %y] to: Lua value "(\(%x as lua expr) > \(%y as lua expr))"
|
||||
compile [%x <= %y] to: Lua value "(\(%x as lua expr) <= \(%y as lua expr))"
|
||||
compile [%x >= %y] to: Lua value "(\(%x as lua expr) >= \(%y as lua expr))"
|
||||
# TODO: optimize case of [%x,%y] = [1,2]
|
||||
compile [%a is %b, %a = %b, %a == %b] to:
|
||||
compile [%a is %b, %a = %b, %a == %b] to
|
||||
lua> ".."
|
||||
local safe = {Text=true, Number=true};
|
||||
local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu);
|
||||
@ -32,7 +32,7 @@ immediately:
|
||||
else
|
||||
return Lua.Value(tree.source, "utils.equivalent(", a_lua, ", ", b_lua, ")");
|
||||
end
|
||||
compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to:
|
||||
compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to
|
||||
lua> ".."
|
||||
local safe = {Text=true, Number=true};
|
||||
local a_lua, b_lua = \%a:as_lua(nomsu), \%b:as_lua(nomsu);
|
||||
@ -45,8 +45,8 @@ immediately:
|
||||
compile [%'s id, id of %] to: Lua value "nomsu.ids[\(% as lua expr)]"
|
||||
|
||||
# Variable assignment operator
|
||||
immediately:
|
||||
compile [%var <- %value] to:
|
||||
immediately
|
||||
compile [%var <- %value] to
|
||||
lua> "local \%var_lua = \%var:as_lua(nomsu);"
|
||||
assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)"
|
||||
lua> "local \%value_lua = \%value:as_lua(nomsu);"
|
||||
@ -58,9 +58,9 @@ immediately:
|
||||
end
|
||||
return lua;
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
# Simultaneous mutli-assignments like: x,y,z = 1,x,3;
|
||||
compile [<- %assignments] to:
|
||||
compile [<- %assignments] to
|
||||
assume ((%assignments' "type") is "Dict") or barf ".."
|
||||
Expected a Dict for the assignments part of '<- %' statement, not \(%assignments' source code)
|
||||
lua> ".."
|
||||
@ -83,20 +83,20 @@ immediately:
|
||||
end
|
||||
return Lua(tree.source, lhs, " = ", rhs, ";");
|
||||
|
||||
immediately:
|
||||
compile [export %var <- %value] to:
|
||||
immediately
|
||||
compile [export %var <- %value] to
|
||||
%var_lua <- (%var as lua)
|
||||
assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)"
|
||||
%value_lua <- (%value as lua)
|
||||
assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)"
|
||||
return: Lua "\(%var_lua) = \(%value_lua);"
|
||||
|
||||
compile [exporting %exported %body] to:
|
||||
compile [exporting %exported %body] to
|
||||
%body_lua <- (%body as lua statements)
|
||||
lua> "\%body_lua:remove_free_vars(unpack(\(%exported.value)));"
|
||||
return %body_lua
|
||||
|
||||
compile [with %assignments %body] to:
|
||||
compile [with %assignments %body] to
|
||||
%lua <- (%body as lua statements)
|
||||
lua> ".."
|
||||
local lhs, rhs = Lua(tree.source), Lua(\%assignments.source);
|
||||
@ -130,7 +130,7 @@ immediately:
|
||||
\%lua
|
||||
end -- 'with' block
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
# Math Operators
|
||||
compile [%x + %y] to: Lua value "(\(%x as lua expr) + \(%y as lua expr))"
|
||||
compile [%x - %y] to: Lua value "(\(%x as lua expr) - \(%y as lua expr))"
|
||||
@ -171,7 +171,7 @@ immediately:
|
||||
compile [length of %list] to: Lua value "(#\(%list as lua expr))"
|
||||
|
||||
# Update operators
|
||||
immediately:
|
||||
immediately
|
||||
parse [%var + <- %, %var +<- %] as: %var <- (%var + %)
|
||||
parse [%var - <- %, %var -<- %] as: %var <- (%var - %)
|
||||
parse [%var * <- %, %var *<- %] as: %var <- (%var * %)
|
||||
|
@ -5,7 +5,7 @@
|
||||
use "core/metaprogramming.nom"
|
||||
|
||||
# Text functions
|
||||
action [%texts joined with %glue]:
|
||||
action [%texts joined with %glue]
|
||||
lua> ".."
|
||||
local text_bits = {}
|
||||
for i,bit in ipairs(\%texts) do text_bits[i] = stringify(bit) end
|
||||
|
@ -28,15 +28,15 @@ lua> ".."
|
||||
end
|
||||
attrdir(".");
|
||||
|
||||
action [sha1 %]:
|
||||
action [sha1 %]
|
||||
lua> "return sha1(\%);"
|
||||
|
||||
action [file with hash %hash]:
|
||||
action [file with hash %hash]
|
||||
%file <- (%hash in %hash_to_filename)
|
||||
assume %file or barf "File with SHA1 hash \%hash not found!"
|
||||
return %file
|
||||
|
||||
action [hash of file %filename]:
|
||||
action [hash of file %filename]
|
||||
lua> ".."
|
||||
local f = io.open(\%filename);
|
||||
local hash = sha1(f:read("*a"));
|
||||
|
@ -1,6 +1,6 @@
|
||||
use "core"
|
||||
|
||||
compile [@%var] to:
|
||||
compile [@%var] to
|
||||
lua> ".."
|
||||
local key_lua = repr(\%var.value);
|
||||
local key_attr = (key_lua:match("'([a-zA-Z][a-zA-Z0-9]*)'")
|
||||
@ -12,7 +12,7 @@ compile [@%var] to:
|
||||
end
|
||||
return {expr="_me["..key_lua.."]"};
|
||||
|
||||
compile [@%var <- %val] to:
|
||||
compile [@%var <- %val] to
|
||||
lua> ".."
|
||||
local val_lua = \(%val as lua expr);
|
||||
local key_lua = repr(\%var.value);
|
||||
@ -25,12 +25,12 @@ compile [@%var <- %val] to:
|
||||
end
|
||||
return {statements="_me["..key_lua.."] = "..val_lua..";"};
|
||||
|
||||
compile [define object %classname %class_body] to:
|
||||
compile [define object %classname %class_body] to
|
||||
%class_identifier <- (=lua "nomsu:var_to_lua_identifier(\(%classname as value)):sub(2,-1)")
|
||||
if: %class_identifier is ""
|
||||
%class_identifier <- "class"
|
||||
%methods <- []
|
||||
for %line in (%class_body's "value"):
|
||||
for %line in (%class_body's "value")
|
||||
if: (%line's "type") is "Comment"
|
||||
do next %line
|
||||
assume (((%line's "type") == "FunctionCall") and ((%line's stub) == "action % %"))
|
||||
@ -86,7 +86,7 @@ compile [define object %classname %class_body] to:
|
||||
return {..}
|
||||
statements:".."
|
||||
do -- \%class_identifier
|
||||
-- Create the class object:
|
||||
-- Create the class object
|
||||
local \%class_identifier = setmetatable({
|
||||
name=\(%classname as lua expr), instances=setmetatable({}, {__mode="k"}),
|
||||
}, {
|
||||
@ -104,10 +104,10 @@ compile [define object %classname %class_body] to:
|
||||
});
|
||||
\%class_identifier.class = \%class_identifier;
|
||||
|
||||
-- Define the methods:
|
||||
-- Define the methods
|
||||
\(%methods joined with "\n")
|
||||
|
||||
-- Define class methods for instantiating and accessing instances:
|
||||
-- Define class methods for instantiating and accessing instances
|
||||
\%class_identifier.instance_metatable = {
|
||||
__index=\%class_identifier,
|
||||
__tostring=\%class_identifier['% as text'] or function(inst)
|
||||
|
@ -2,7 +2,7 @@ use "core"
|
||||
|
||||
compile [@] to: Lua value "self"
|
||||
|
||||
compile [@%var] to:
|
||||
compile [@%var] to
|
||||
lua> ".."
|
||||
local key_lua = repr(\%var.value);
|
||||
local key_attr = (key_lua:match("'([a-zA-Z][a-zA-Z0-9]*)'")
|
||||
@ -14,7 +14,7 @@ compile [@%var] to:
|
||||
end
|
||||
return Lua.Value(tree.source, "self["..key_lua.."]");
|
||||
|
||||
compile [@%var <- %val] to:
|
||||
compile [@%var <- %val] to
|
||||
lua> ".."
|
||||
local val_lua = \(%val as lua expr);
|
||||
local key_lua = repr(\%var.value);
|
||||
@ -27,7 +27,7 @@ compile [@%var <- %val] to:
|
||||
end
|
||||
return Lua(tree.source, "self[", key_lua, "] = ", val_lua, ";");
|
||||
|
||||
compile [as %instance %body] to:
|
||||
compile [as %instance %body] to
|
||||
%body_lua <- (%body as lua)
|
||||
lua> "\%body_lua:convert_to_statements();"
|
||||
return
|
||||
@ -43,21 +43,21 @@ compile [as %instance %body] to:
|
||||
\%body_lua
|
||||
end
|
||||
|
||||
compile [define object %classname %class_body] to:
|
||||
compile [define object %classname %class_body] to
|
||||
%class_identifier <- (=lua "nomsu:var_to_lua_identifier(\(%classname as value)):sub(2,-1)")
|
||||
if: %class_identifier is ""
|
||||
%class_identifier <- "class"
|
||||
%methods <- []
|
||||
%__index <- %class_identifier
|
||||
%__newindex <- "nil"
|
||||
for %line in (%class_body's "value"):
|
||||
for %line in (%class_body's "value")
|
||||
if: (%line's "type") is "Comment"
|
||||
do next %line
|
||||
if: ((%line's "type") is "FunctionCall") and ((%line's stub) is "slots %")
|
||||
%slot_index_clauses <- []
|
||||
%slot_newindex_clauses <- []
|
||||
%slots <- %line.value.2.value
|
||||
for %slot_index = %slot_var in %slots:
|
||||
for %slot_index = %slot_var in %slots
|
||||
to %slot_index_clauses add ".."
|
||||
if key == \(repr (%slot_var's "value")) or key == \(repr (%slot_var as lua expr)) then
|
||||
return rawget(self, \%slot_index);
|
||||
@ -115,7 +115,7 @@ compile [define object %classname %class_body] to:
|
||||
return
|
||||
Lua ".."
|
||||
do -- \%class_identifier
|
||||
-- Create the class object:
|
||||
-- Create the class object
|
||||
local \%class_identifier = setmetatable({
|
||||
name=\(%classname as lua expr), instances=setmetatable({}, {__mode="k"}),
|
||||
}, {
|
||||
@ -132,10 +132,10 @@ compile [define object %classname %class_body] to:
|
||||
});
|
||||
\%class_identifier.class = \%class_identifier;
|
||||
|
||||
-- Define the methods:
|
||||
-- Define the methods
|
||||
\(%methods joined with "\n")
|
||||
|
||||
-- Define class methods for instantiating and accessing instances:
|
||||
-- Define class methods for instantiating and accessing instances
|
||||
\%class_identifier.instance_metatable = {
|
||||
__index=\%__index,
|
||||
__newindex=\%__newindex,
|
||||
|
@ -26,19 +26,19 @@ parse [error!, panic!, fail!, abort!] as: barf!
|
||||
parse [error %, panic %, fail %, abort %] as: barf %
|
||||
parse [assert %condition %message] as: assume %condition or barf %message
|
||||
parse [%cond ? %if_true %if_false] as: %if_true if %cond else %if_false
|
||||
compile [function %args %body, lambda %args %body] to:
|
||||
%body_lua <- (%body as lua)
|
||||
%statements <- ((%body_lua's "statements") or "return \(%body_lua's "expr");")
|
||||
%locals <- (% for all ((%body_lua's "locals") or []))
|
||||
for all (%args's "value"):
|
||||
lua> "utils.remove_from_list(\%locals, \(% as lua expr));"
|
||||
if: (size of %locals) > 0
|
||||
%statements <- "local \(%locals joined with ", ");\n\%statements"
|
||||
|
||||
return {..}
|
||||
expr: ".."
|
||||
(function(\(((% as lua expr) for all (%args's "value")) joined with ", "))
|
||||
\%statements
|
||||
end)
|
||||
compile [function %args %body, lambda %args %body] to
|
||||
%lua <-: Lua value "(function("
|
||||
for %i = %arg in %args.value
|
||||
if: %i > 1
|
||||
to %lua write ", "
|
||||
to %lua write: %arg as lua expr
|
||||
to %lua write ")\n "
|
||||
%body <-: %body as lua
|
||||
lua> "\%body:convert_to_statements('return ');"
|
||||
for all %args.value: lua> "\%body:remove_free_vars(\%);"
|
||||
to %lua write %body
|
||||
to %lua write "\nend)"
|
||||
return %lua
|
||||
parse [function %name %args %body] as: %name <- (function %args %body)
|
||||
compile [call %fn %args] to {expr:"\(%fn as lua expr)(unpack(\(%args as lua expr)))"}
|
||||
compile [call %fn %args] to
|
||||
Lua value "\(%fn as lua expr)(unpack(\(%args as lua expr)))"
|
||||
|
@ -10,7 +10,7 @@ shebang: "#!" [^%nl]* (!. / %nl)
|
||||
statement: action / expression
|
||||
|
||||
indented_block (Block):
|
||||
{| (":" / "(..)")? indent
|
||||
{| indent
|
||||
statement (nodent statement)*
|
||||
(dedent / (("" -> "Error while parsing block") => error))
|
||||
|} -> Tuple
|
||||
|
@ -14,6 +14,7 @@ do
|
||||
local _obj_0 = require("lua_obj")
|
||||
Lua, Nomsu, Location = _obj_0.Lua, _obj_0.Nomsu, _obj_0.Location
|
||||
end
|
||||
local MAX_LINE = 80
|
||||
local Types = { }
|
||||
Types.DictEntry = immutable({
|
||||
"key",
|
||||
@ -307,21 +308,24 @@ Tree("Action", {
|
||||
return nomsu
|
||||
else
|
||||
local inline_version = self:as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80 then
|
||||
if inline_version and #inline_version <= MAX_LINE then
|
||||
return inline_version
|
||||
end
|
||||
local nomsu = Nomsu(self.source)
|
||||
local spacer = nil
|
||||
for i, bit in ipairs(self.value) do
|
||||
if spacer then
|
||||
nomsu:append(spacer)
|
||||
end
|
||||
if bit.type == "Word" then
|
||||
if spacer then
|
||||
nomsu:append(spacer)
|
||||
end
|
||||
nomsu:append(bit.value)
|
||||
spacer = " "
|
||||
else
|
||||
local arg_nomsu = bit:as_nomsu(true)
|
||||
if arg_nomsu and #arg_nomsu < 80 then
|
||||
if arg_nomsu and #arg_nomsu < MAX_LINE then
|
||||
if spacer then
|
||||
nomsu:append(spacer)
|
||||
end
|
||||
if bit.type == "Action" or bit.type == "Block" then
|
||||
arg_nomsu:parenthesize()
|
||||
end
|
||||
@ -333,6 +337,10 @@ Tree("Action", {
|
||||
end
|
||||
if bit.type == "Action" or bit.type == "Block" then
|
||||
nomsu:append("\n ")
|
||||
else
|
||||
if spacer then
|
||||
nomsu:append(spacer)
|
||||
end
|
||||
end
|
||||
spacer = "\n.."
|
||||
end
|
||||
@ -423,7 +431,7 @@ Tree("Text", {
|
||||
return nomsu
|
||||
else
|
||||
local inline_version = self:as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80 then
|
||||
if inline_version and #inline_version <= MAX_LINE then
|
||||
return inline_version
|
||||
end
|
||||
local nomsu = Nomsu(self.source, '".."\n ')
|
||||
@ -472,7 +480,7 @@ Tree("List", {
|
||||
line_length = line_length + #last_line
|
||||
end
|
||||
if i < #self.value then
|
||||
if line_length >= 80 then
|
||||
if line_length >= MAX_LINE then
|
||||
lua:append(",\n")
|
||||
line_length = 0
|
||||
else
|
||||
@ -504,7 +512,7 @@ Tree("List", {
|
||||
return nomsu
|
||||
else
|
||||
local inline_version = self:as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80 then
|
||||
if inline_version and #inline_version <= MAX_LINE then
|
||||
return inline_version
|
||||
end
|
||||
local nomsu = Nomsu(self.source, "[..]")
|
||||
@ -513,7 +521,7 @@ Tree("List", {
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local item = _list_0[_index_0]
|
||||
local item_nomsu = item:as_nomsu(true)
|
||||
if item_nomsu and #line + #", " + #item_nomsu <= 80 then
|
||||
if item_nomsu and #line + #", " + #item_nomsu <= MAX_LINE then
|
||||
if #line.bits > 1 then
|
||||
line:append(", ")
|
||||
end
|
||||
@ -569,7 +577,7 @@ Tree("Dict", {
|
||||
line_length = line_length + #last_line
|
||||
end
|
||||
if i < #self.value then
|
||||
if line_length >= 80 then
|
||||
if line_length >= MAX_LINE then
|
||||
lua:append(",\n")
|
||||
line_length = 0
|
||||
else
|
||||
@ -624,7 +632,7 @@ Tree("Dict", {
|
||||
key_nomsu:parenthesize()
|
||||
end
|
||||
local value_nomsu = entry.value:as_nomsu(true)
|
||||
if value_nomsu and #line + #", " + #key_nomsu + #":" + #value_nomsu <= 80 then
|
||||
if value_nomsu and #line + #", " + #key_nomsu + #":" + #value_nomsu <= MAX_LINE then
|
||||
if #line.bits > 1 then
|
||||
line:append(", ")
|
||||
end
|
||||
|
@ -8,6 +8,7 @@ immutable = require 'immutable'
|
||||
{:insert, :remove, :concat} = table
|
||||
{:Lua, :Nomsu, :Location} = require "lua_obj"
|
||||
|
||||
MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
|
||||
|
||||
Types = {}
|
||||
Types.DictEntry = immutable({"key","value"}, {name:"DictEntry"})
|
||||
@ -177,20 +178,19 @@ Tree "Action",
|
||||
return nomsu
|
||||
else
|
||||
inline_version = @as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80
|
||||
if inline_version and #inline_version <= MAX_LINE
|
||||
return inline_version
|
||||
nomsu = Nomsu(@source)
|
||||
spacer = nil
|
||||
for i,bit in ipairs @value
|
||||
if spacer
|
||||
nomsu\append spacer
|
||||
|
||||
if bit.type == "Word"
|
||||
if spacer then nomsu\append spacer
|
||||
nomsu\append bit.value
|
||||
spacer = " "
|
||||
else
|
||||
arg_nomsu = bit\as_nomsu(true)
|
||||
if arg_nomsu and #arg_nomsu < 80
|
||||
if arg_nomsu and #arg_nomsu < MAX_LINE
|
||||
if spacer then nomsu\append spacer
|
||||
if bit.type == "Action" or bit.type == "Block"
|
||||
arg_nomsu\parenthesize!
|
||||
spacer = " "
|
||||
@ -199,6 +199,8 @@ Tree "Action",
|
||||
return nil unless nomsu
|
||||
if bit.type == "Action" or bit.type == "Block"
|
||||
nomsu\append "\n "
|
||||
else
|
||||
if spacer then nomsu\append spacer
|
||||
spacer = "\n.."
|
||||
nomsu\append arg_nomsu
|
||||
return nomsu
|
||||
@ -253,7 +255,7 @@ Tree "Text",
|
||||
return nomsu
|
||||
else
|
||||
inline_version = @as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80
|
||||
if inline_version and #inline_version <= MAX_LINE
|
||||
return inline_version
|
||||
nomsu = Nomsu(@source, '".."\n ')
|
||||
for i, bit in ipairs @value
|
||||
@ -290,7 +292,7 @@ Tree "List",
|
||||
else
|
||||
line_length += #last_line
|
||||
if i < #@value
|
||||
if line_length >= 80
|
||||
if line_length >= MAX_LINE
|
||||
lua\append ",\n"
|
||||
line_length = 0
|
||||
else
|
||||
@ -312,13 +314,13 @@ Tree "List",
|
||||
return nomsu
|
||||
else
|
||||
inline_version = @as_nomsu(true)
|
||||
if inline_version and #inline_version <= 80
|
||||
if inline_version and #inline_version <= MAX_LINE
|
||||
return inline_version
|
||||
nomsu = Nomsu(@source, "[..]")
|
||||
line = Nomsu(@source, "\n ")
|
||||
for item in *@value
|
||||
item_nomsu = item\as_nomsu(true)
|
||||
if item_nomsu and #line + #", " + #item_nomsu <= 80
|
||||
if item_nomsu and #line + #", " + #item_nomsu <= MAX_LINE
|
||||
if #line.bits > 1
|
||||
line\append ", "
|
||||
line\append item_nomsu
|
||||
@ -365,7 +367,7 @@ Tree "Dict",
|
||||
else
|
||||
line_length += #last_line
|
||||
if i < #@value
|
||||
if line_length >= 80
|
||||
if line_length >= MAX_LINE
|
||||
lua\append ",\n"
|
||||
line_length = 0
|
||||
else
|
||||
@ -400,7 +402,7 @@ Tree "Dict",
|
||||
if entry.key.type == "Action" or entry.key.type == "Block"
|
||||
key_nomsu\parenthesize!
|
||||
value_nomsu = entry.value\as_nomsu(true)
|
||||
if value_nomsu and #line + #", " + #key_nomsu + #":" + #value_nomsu <= 80
|
||||
if value_nomsu and #line + #", " + #key_nomsu + #":" + #value_nomsu <= MAX_LINE
|
||||
if #line.bits > 1
|
||||
line\append ", "
|
||||
line\append key_nomsu,":",value_nomsu
|
||||
|
@ -2,16 +2,16 @@ use "core"
|
||||
use "lib/object2.nom"
|
||||
|
||||
#..
|
||||
immediately:
|
||||
define object "Dog":
|
||||
action [bark]:
|
||||
immediately
|
||||
define object "Dog"
|
||||
action [bark]
|
||||
%barks <- ("Bark!" for all 1 to (@%barks))
|
||||
return (%barks joined with " ")
|
||||
action [get pissed off]:
|
||||
action [get pissed off]
|
||||
(@%barks) +<- 1
|
||||
|
||||
%d <- (new Dog {barks:2})
|
||||
as %d:
|
||||
as %d
|
||||
assume ((@) = %d)
|
||||
assume ((@%barks) = 2)
|
||||
assume ((bark) = "Bark! Bark!")
|
||||
|
@ -14,11 +14,11 @@ assume (("x" + "y") = "xy")
|
||||
|
||||
say "Text test passed."
|
||||
|
||||
immediately:
|
||||
immediately
|
||||
parse [アクション %spec %body] as: action %spec %body
|
||||
|
||||
%こんにちは <- "こんにちは"
|
||||
アクション [% と言う]:
|
||||
アクション [% と言う]
|
||||
"\(%)世界"
|
||||
|
||||
assume ((%こんにちは と言う) = "こんにちは世界") or barf "Unicode doesn't work"
|
||||
|
Loading…
Reference in New Issue
Block a user