From d5cfaa37be9e278c44a25ef448a071390597306e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 18 Jul 2018 01:27:56 -0700 Subject: Upgrading to version 2.3 (main change: "=" instead of "<-" for assignment) --- core/collections.nom | 94 ++++++++++++++--------------- core/control_flow.nom | 154 +++++++++++++++++++++++++---------------------- core/coroutines.nom | 2 +- core/errors.nom | 2 +- core/io.nom | 2 +- core/math.nom | 30 ++++----- core/metaprogramming.nom | 12 ++-- core/operators.nom | 41 ++++++------- core/scopes.nom | 19 +++--- core/text.nom | 4 +- 10 files changed, 182 insertions(+), 178 deletions(-) (limited to 'core') diff --git a/core/collections.nom b/core/collections.nom index d99ae49..45ca74c 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains code that supports manipulating and using collections like lists and dictionaries. @@ -34,22 +34,22 @@ action [..] for %key = %value in %list: if (%key is %item): return (no) return (yes) -test: assume ({"x": no} has key "x") +test: assume ({x: no} has key "x") parse [%list has key %index, %list has index %index] as (%list.%index != (nil)) test: - assume ({"x": no} doesn't have key "y") - assume (not ({"x": no} doesn't have key "x")) + assume ({x: no} doesn't have key "y") + assume (not ({x: no} doesn't have key "x")) parse [..] %list doesn't have key %index, %list does not have key %index %list doesn't have index %index, %list does not have index %index -..as (%list.%index = (nil)) +..as (%list.%index == (nil)) compile [number of keys in %list] to (..) Lua value "utils.size(\(%list as lua expr))" test: - %list <- [1, 2, 3, 4, 5] + %list = [1, 2, 3, 4, 5] append 6 to %list assume ((last in %list) is 6) pop from %list @@ -74,10 +74,10 @@ compile [remove index %index from %list] to (..) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # List Comprehension -test: assume (((% * %) for % in [1, 2, 3]) = [1, 4, 9]) +test: assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9]) parse [%expression for %item in %iterable] as (..) result of: - %comprehension <- [] + %comprehension = [] for %item in %iterable: add %expression to %comprehension return %comprehension @@ -86,47 +86,46 @@ parse [..] %expression for %index in %start to %stop by %step ..as (..) result of: - %comprehension <- [] + %comprehension = [] for %index in %start to %stop via %step: add %expression to %comprehension return %comprehension ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -test: assume (((% * %) for % in 1 to 3) = [1, 4, 9]) +test: assume (((% * %) for % in 1 to 3) == [1, 4, 9]) parse [%expression for %var in %start to %stop] as (..) %expression for %var in %start to %stop via 1 -test: assume (("\%k,\%v" for %k = %v in {"x": 1}) = ["x,1"]) +test: assume (("\%k,\%v" for %k = %v in {x: 1}) == ["x,1"]) parse [..] %expression for %key = %value in %iterable %expression for %key %value in %iterable ..as (..) result of: - %comprehension <- [] + %comprehension = [] for %key = %value in %iterable: add %expression to %comprehension return %comprehension # Dict comprehensions -test: assume (((% * %) = % for % in [1, 2, 3]) = {1: 1, 4: 2, 9: 3}) +test: assume (((% * %) = % for % in [1, 2, 3]) == {1: 1, 4: 2, 9: 3}) parse [%key = %value for %item in %iterable, %key %value for %item in %iterable] ..as (..) result of: - %comprehension <- {} - for %item in %iterable: %comprehension.%key <- %value + %comprehension = {} + for %item in %iterable: %comprehension.%key = %value return %comprehension test: - assume (..) - (%k = (%v * %v) for %k = %v in {"x": 1, "y": 2, "z": 3}) = {"x": 1, "y": 4, "z": 9} + assume ((%k = (%v * %v) for %k = %v in {x: 1, y: 2, z: 3}) == {x: 1, y: 4, z: 9}) parse [..] %key = %value for %src_key = %src_value in %iterable %key %value for %src_key %src_value in %iterable ..as (..) result of: - %comprehension <- {} - for %src_key = %src_value in %iterable: %comprehension.%key <- %value + %comprehension = {} + for %src_key = %src_value in %iterable: %comprehension.%key = %value return %comprehension parse [..] @@ -134,41 +133,41 @@ parse [..] %key %value for %item in %start to %stop via %step ..as (..) result of: - %comprehension <- {} - for %item in %start to %stop via %step: %comprehension.%key <- %value + %comprehension = {} + for %item in %start to %stop via %step: %comprehension.%key = %value return %comprehension ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -test: assume (((% * %) = % for % in 1 to 3) = {1: 1, 4: 2, 9: 3}) +test: assume (((% * %) = % for % in 1 to 3) == {1: 1, 4: 2, 9: 3}) parse [..] %key = %value for %item in %start to %stop %key %value for %item in %start to %stop ..as (%key = %value for %item in %start to %stop via 1) -test: assume (([[1, 2], [3, 4]] flattened) = [1, 2, 3, 4]) +test: assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4]) action [%lists flattened]: - %flat <- [] + %flat = [] for %list in %lists: for %item in %list: add %item to %flat return %flat -test: assume ((entries in {"x": 1}) = [{"key": "x", "value": 1}]) -parse [entries in %dict] as ({"key": %k, "value": %v} for %k = %v in %dict) -test: assume ((keys in {"x": 1}) = ["x"]) +test: assume ((entries in {x: 1}) == [{key: "x", value: 1}]) +parse [entries in %dict] as ({key: %k, value: %v} for %k = %v in %dict) +test: assume ((keys in {x: 1}) == ["x"]) parse [keys in %dict, keys of %dict] as (%k for %k = %v in %dict) -test: assume ((values in {"x": 1}) = [1]) +test: assume ((values in {x: 1}) == [1]) parse [values in %dict, values of %dict] as (%v for %k = %v in %dict) # Metatable stuff test: - %t <- {} - set %t 's metatable to {"__tostring": [%] -> "XXX"} - assume ("\%t" = "XXX") + %t = {} + set %t 's metatable to {__tostring: [%] -> "XXX"} + assume ("\%t" == "XXX") compile [set %dict 's metatable to %metatable] to (..) Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));" -test: assume (({} with fallback % -> (% + 1)).10 = 11) +test: assume (({} with fallback % -> (% + 1)).10 == 11) compile [%dict with fallback %key -> %value] to (..) Lua value ".." setmetatable(\(%dict as lua expr), {__index=function(self, \(%key as lua expr)) @@ -180,41 +179,38 @@ compile [%dict with fallback %key -> %value] to (..) # Sorting test: - %x <- [3, 1, 2] + %x = [3, 1, 2] sort %x - assume (%x = [1, 2, 3]) + assume (%x == [1, 2, 3]) sort %x by % = (- %) - assume (%x = [3, 2, 1]) - %keys <- {1: 999, 2: 0, 3: 50} + assume (%x == [3, 2, 1]) + %keys = {1: 999, 2: 0, 3: 50} sort %x by % = %keys.% - assume (%x = [2, 3, 1]) + assume (%x == [2, 3, 1]) compile [sort %items] to (Lua "table.sort(\(%items as lua expr));") parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (..) do: - %keys <- ({} with fallback %item -> %key_expr) + %keys = ({} with fallback %item -> %key_expr) lua> "table.sort(\%items, function(x,y) return \%keys[x] < \%keys[y] end)" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -test: assume ((sorted [3, 1, 2]) = [1, 2, 3]) +test: assume ((sorted [3, 1, 2]) == [1, 2, 3]) action [%items sorted, sorted %items]: - %copy <- (% for % in %items) + %copy = (% for % in %items) sort %copy return %copy parse [%items sorted by %item = %key, %items sorted by %item -> %key] as (..) result of: - %copy <- (% for % in %items) + %copy = (% for % in %items) sort %copy by %item = %key return %copy -test: assume ((unique [1, 2, 1, 3, 2, 3]) = [1, 2, 3]) +test: assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3]) action [unique %items]: - %unique <- [] - %seen <- {} - for % in %items: - unless %seen.%: - add % to %unique - %seen.% <- (yes) - return %unique + %unique = [] + %seen = {} + for % in %items: unless %seen.% (: add % to %unique; %seen.% = (yes)) + return %unique \ No newline at end of file diff --git a/core/control_flow.nom b/core/control_flow.nom index bb30e49..b3f0978 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains compile-time actions that define basic control flow structures like "if" statements and loops. @@ -15,10 +15,7 @@ compile [do nothing] to (Lua "") # Conditionals test: if (no): barf "conditional fail" compile [if %condition %if_body] to (..) - Lua ".." - if \(%condition as lua expr) then - \(%if_body as lua statements) - end + Lua "if \(%condition as lua expr) then\n \(%if_body as lua statements)\nend" test: unless (yes): barf "conditional fail" parse [unless %condition %unless_body] as (if (not %condition) %unless_body) @@ -43,10 +40,9 @@ compile [..] %when_false_expr unless %condition else %when_true_expr %when_false_expr unless %condition then %when_true_expr ..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 {"Text": yes, "List": yes, "Dict": yes, "Number": yes}.(%when_true_expr.type): + if {Text: yes, List: yes, Dict: yes, Number: yes}.(%when_true_expr.type): return (..) Lua value ".." (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr)) @@ -65,13 +61,11 @@ compile [..] end end)()) - # GOTOs compile [=== %label ===, --- %label ---, *** %label ***] to (..) Lua "::label_\(%label as lua identifier)::" -compile [go to %label] to (..) - Lua "goto label_\(%label as lua identifier)" +compile [go to %label] to (Lua "goto label_\(%label as lua identifier)") # Basic loop control compile [do next] to (Lua "continue") @@ -96,18 +90,19 @@ compile [%tree has subtree %subtree where %condition] to (..) compile [do next repeat] to (Lua "goto continue_repeat") compile [stop repeating] to (Lua "goto stop_repeat") compile [repeat while %condition %body] to: - %lua <- (..) - Lua ".." - while \(%condition as lua expr) do - \(%body as lua statements) + %lua = (..) + Lua "while \(%condition as lua expr) do\n \(%body as lua statements)" if (..) - %body has subtree % where ((%.type = "Action") and (%.stub is "do next repeat")) - ..: to %lua write "\n ::continue_repeat::" + %body has subtree % where ((%.type == "Action") and (%.stub is "do next repeat")) + ..: + to %lua write "\n ::continue_repeat::" to %lua write "\nend --while-loop" - if (%body has subtree % where ((%.type = "Action") and (%.stub is "stop repeating"))): - %lua <- (..) + if (..) + %body has subtree % where ((%.type == "Action") and (%.stub is "stop repeating")) + ..: + %lua = (..) Lua ".." do -- scope of "stop repeating" label \%lua @@ -119,13 +114,19 @@ compile [repeat while %condition %body] to: parse [repeat %body] as (repeat while (yes) %body) parse [repeat until %condition %body] as (repeat while (not %condition) %body) compile [repeat %n times %body] to: - %lua <- (Lua "for i=1,\(%n as lua expr) do\n \(%body as lua statements)") - if (%body has subtree % where ((%.type = "Action") and (%.stub is "do next repeat"))): + %lua = (..) + Lua "for i=1,\(%n as lua expr) do\n \(%body as lua statements)" + + if (..) + %body has subtree % where ((%.type == "Action") and (%.stub is "do next repeat")) + ..: to %lua write "\n ::continue_repeat::" to %lua write "\nend --numeric for-loop" - if (%body has subtree % where ((%.type = "Action") and (%.stub is "stop repeating"))): - %lua <- (..) + if (..) + %body has subtree % where ((%.type == "Action") and (%.stub is "stop repeating")) + ..: + %lua = (..) Lua ".." do -- scope of "stop repeating" label \%lua @@ -151,24 +152,26 @@ compile [..] for %var in %start to %stop by %step %body for %var in %start to %stop via %step %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" - %lua <- (..) + %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 (..) - (%.type = "Action") and ((%.stub is "do next %") and (%.(3).1 = %var.1)) - ..: to %lua write "\n \(compile as (===next %var ===))" + (%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %var.1)) + ..: + to %lua write "\n \(compile as (===next %var ===))" to %lua write "\nend --numeric for-loop" if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "stop %") and (%.(2).1 = %var.1)) + (%.type == "Action") and ((%.stub is "stop %") and (%.(2).1 == %var.1)) ..: - %lua <- (..) + %lua = (..) Lua ".." do -- scope for stopping for-loop \%lua @@ -185,24 +188,27 @@ parse [for %var in %start to %stop %body] as (..) # For-each loop (lua's "ipairs()") 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" - %lua <- (..) + %lua = (..) Lua ".." for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do \(%body as lua statements) if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "do next %") and (%.(3).1 = %var.1)) - ..: to %lua write (Lua "\n\(compile as (===next %var ===))") + (%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %var.1)) + ..: + to %lua write (..) + Lua "\n\(compile as (===next %var ===))" to %lua write "\nend --foreach-loop" if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "stop %") and (%.(2).1 = %var.1)) + (%.type == "Action") and ((%.stub is "stop %") and (%.(2).1 == %var.1)) ..: - %lua <- (..) + %lua = (..) Lua ".." do -- scope for stopping for-loop \%lua @@ -213,39 +219,47 @@ compile [for %var in %iterable %body] to: # Dict iteration (lua's "pairs()") -compile [for %key = %value in %iterable %body, for %key %value in %iterable %body] to: +compile [for %key = %value in %iterable %body, 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" assume (%value.type is "Var") or barf "Loop expected variable, not: \%value" - %lua <- (..) + %lua = (..) 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 (..) - (%.type = "Action") and ((%.stub is "do next %") and (%.(3).1 = %key.1)) - ..: to %lua write (Lua "\n\(compile as (===next %key ===))") + (%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %key.1)) + ..: + to %lua write (..) + Lua "\n\(compile as (===next %key ===))" if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "do next %") and (%.(3).1 = %value.1)) - ..: to %lua write (Lua "\n\(compile as (===next %value ===))") + (%.type == "Action") and ((%.stub is "do next %") and (%.(3).1 == %value.1)) + ..: + to %lua write (..) + Lua "\n\(compile as (===next %value ===))" to %lua write "\nend --foreach-loop" - %stop_labels <- (Lua "") + %stop_labels = (Lua "") if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "stop %") and (%.(2).1 = %key.1)) - ..: to %stop_labels write "\n\(compile as (===stop %key ===))" + (%.type == "Action") and ((%.stub is "stop %") and (%.(2).1 == %key.1)) + ..: + to %stop_labels write "\n\(compile as (===stop %key ===))" if (..) %body has subtree % where (..) - (%.type = "Action") and ((%.stub is "stop %") and (%.(2).1 = %value.1)) - ..: to %stop_labels write "\n\(compile as (===stop %value ===))" + (%.type == "Action") and ((%.stub is "stop %") and (%.(2).1 == %value.1)) + ..: + to %stop_labels write "\n\(compile as (===stop %value ===))" if ((length of "\%stop_labels") > 0): - %lua <- (..) + %lua = (..) Lua "do -- scope for stopping for % = % loop\n \%lua\%stop_labels\nend" return %lua @@ -254,15 +268,15 @@ compile [for %key = %value in %iterable %body, for %key %value in %iterable %bod # Switch statement/multi-branch if compile [when %body] to: - %code <- (Lua "") - %fallthroughs <- [] - %is_first <- (yes) - %seen_else <- (no) - %branches <- (%body if (%body.type = "Block") else [%body]) + %code = (Lua "") + %fallthroughs = [] + %is_first = (yes) + %seen_else = (no) + %branches = (%body if (%body.type == "Block") else [%body]) for %func_call in %branches: assume (%func_call.type is "Action") or barf "Invalid format for 'when' statement. Only '*' blocks are allowed." with {%star: %func_call.1, %condition: %func_call.2, %action: %func_call.3}: - assume (%star = "*") or barf "Invalid format for 'when' statement. Lines must begin with '*'" + assume (%star == "*") or barf "Invalid format for 'when' statement. Lines must begin with '*'" assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition \ ..or the word "else" @@ -271,11 +285,11 @@ compile [when %body] to: lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" do next %func_call - if (%condition = "else"): + if (%condition == "else"): 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) - %seen_else <- (yes) + %seen_else = (yes) ..else: assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block" lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" @@ -287,26 +301,26 @@ compile [when %body] to: to %code write " then\n " to %code write (%action as lua statements) - %fallthroughs <- [] - %is_first <- (no) + %fallthroughs = [] + %is_first = (no) - assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block" + assume (%fallthroughs == []) or barf "Unfinished fallthrough conditions in 'when' block" assume ((length of "\%code") > 0) or barf "Empty body for 'when' block" to %code write "\nend --when" return %code # Switch statement -compile [when %branch_value = ? %body, when %branch_value is ? %body] to: - %code <- (Lua "") - %fallthroughs <- [] - %is_first <- (yes) - %seen_else <- (no) - %branches <- (%body if (%body.type = "Block") else [%body]) +compile [when %branch_value = ? %body, when %branch_value is? %body] to: + %code = (Lua "") + %fallthroughs = [] + %is_first = (yes) + %seen_else = (no) + %branches = (%body if (%body.type == "Block") else [%body]) for %func_call in %branches: assume (%func_call.type is "Action") or barf "Invalid format for 'when' statement. Only '*' blocks are allowed." with {%star: %func_call.1, %condition: %func_call.2, %action: %func_call.3}: - assume (%star = "*") or barf "Invalid format for 'when' statement. Lines must begin with '*'" + assume (%star == "*") or barf "Invalid format for 'when' statement. Lines must begin with '*'" assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition \ ..or the word "else" @@ -315,7 +329,7 @@ compile [when %branch_value = ? %body, when %branch_value is ? %body] to: lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" do next %func_call - if (%condition = "else"): + if (%condition == "else"): 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) @@ -332,13 +346,13 @@ compile [when %branch_value = ? %body, when %branch_value is ? %body] to: to %code write "then\n " to %code write (%action as lua statements) - %fallthroughs <- [] - %is_first <- (no) + %fallthroughs = [] + %is_first = (no) - assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block" + assume (%fallthroughs == []) or barf "Unfinished fallthrough conditions in 'when' block" assume ((length of "\%code") > 0) or barf "No body for 'when % = ?' block!" to %code write "\nend" - %code <- (..) + %code = (..) Lua ".." do --when % = ? local branch_value = \(%branch_value as lua expr) @@ -350,10 +364,8 @@ compile [when %branch_value = ? %body, when %branch_value is ? %body] to: # Do/finally compile [do %action] to (..) - Lua ".." - do - \(%action as lua statements) - end --do + Lua "do\n \(%action as lua statements)\nend --do" + compile [do %action then always %final_action] to (..) Lua ".." do diff --git a/core/coroutines.nom b/core/coroutines.nom index c14042c..54889d4 100644 --- a/core/coroutines.nom +++ b/core/coroutines.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file defines the code that creates and manipulates coroutines diff --git a/core/errors.nom b/core/errors.nom index 42348a9..efa69a8 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains basic error reporting code diff --git a/core/io.nom b/core/io.nom index e04c88b..2659b3b 100644 --- a/core/io.nom +++ b/core/io.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains basic input/output code diff --git a/core/math.nom b/core/math.nom index 9d432e7..d8952f6 100644 --- a/core/math.nom +++ b/core/math.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file defines some common math literals and functions @@ -57,7 +57,7 @@ compile [all of %items, all %items] to: unless (%items.type is "List"): return (Lua value "utils.all(\(%items as lua expr))") - %clauses <- ((% as lua expr) for % in %items) + %clauses = ((% as lua expr) for % in %items) return (Lua value "(\(%clauses joined with " and "))") parse [not all of %items, not all %items] as (not (all of %items)) @@ -65,7 +65,7 @@ compile [any of %items, any %items] to: unless (%items.type is "List"): return (Lua value "utils.any(\(%items as lua expr))") - %clauses <- ((% as lua expr) for % in %items) + %clauses = ((% as lua expr) for % in %items) return (Lua value "(\(%clauses joined with " or "))") parse [none of %items, none %items] as (not (any of %items)) @@ -73,14 +73,14 @@ compile [sum of %items, sum %items] to: unless (%items.type is "List"): return (Lua value "utils.sum(\(%items as lua expr))") - %clauses <- ((% as lua expr) for % in %items) + %clauses = ((% as lua expr) for % in %items) return (Lua value "(\(%clauses joined with " + "))") compile [product of %items, product %items] to: unless (%items.type is "List"): return (Lua value "utils.product(\(%items as lua expr))") - %clauses <- ((% as lua expr) for % in %items) + %clauses = ((% as lua expr) for % in %items) return (Lua value "(\(%clauses joined with " * "))") action [avg of %items, average of %items] (=lua "(utils.sum(\%items)/#\%items)") @@ -92,21 +92,21 @@ compile [max of %items, biggest of %items, largest of %items, highest of %items] parse [min of %items by %item = %value_expr] as (..) result of: - <- {%best: nil, %best_key: nil} + set {%best: nil, %best_key: nil} for %item in %items: - %key <- %value_expr - if ((%best = (nil)) or (%key < %best_key)): - <- {%best: %item, %best_key: %key} + %key = %value_expr + if ((%best == (nil)) or (%key < %best_key)): + set {%best: %item, %best_key: %key} return %best parse [max of %items by %item = %value_expr] as (..) result of: - <- {%best: nil, %best_key: nil} + set {%best: nil, %best_key: nil} for %item in %items: - %key <- %value_expr - if ((%best = (nil)) or (%key > %best_key)): - <- {%best: %item, %best_key: %key} + %key = %value_expr + if ((%best == (nil)) or (%key > %best_key)): + set {%best: %item, %best_key: %key} return %best @@ -123,5 +123,5 @@ compile [random int %n, random integer %n, randint %n] to (..) compile [random from %low to %high, random number from %low to %high, rand %low %high] ..to (Lua value "math.random(\(%low as lua expr), \(%high as lua expr))") -action [random choice from %elements, random choice %elements, random %elements] (..) - =lua "\%elements[math.random(#\%elements)]" +action [random choice from %elements, random choice %elements, random %elements] +..(=lua "\%elements[math.random(#\%elements)]") \ No newline at end of file diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index c81eb74..8328d26 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This File contains actions for making actions and compile-time actions and some helper functions to make that easier. @@ -13,7 +13,7 @@ lua> ".." local body_lua = AST.is_syntax_tree(\%body) and nomsu:compile(\%body):as_statements("return ") or \%body body_lua:remove_free_vars(lua_args) body_lua:declare_locals() - lua:append(")\n ", body_lua, "\nend") + lua:append(")\\n ", body_lua, "\\nend") return lua end @@ -37,7 +37,8 @@ lua> ".." "] = ", \(compile as (%args -> %body))) for i=2,#\%actions do local alias = \%actions[i] - local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) end))} + local \%alias_args = {"nomsu", "tree", unpack(table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) \ + ..end))} lua:append("\\nnomsu.COMPILE_ACTIONS[", repr(alias.stub), "] = ") if utils.equivalent(\%args, \%alias_args) then lua:append("nomsu.COMPILE_ACTIONS[", repr(\%actions[1].stub), "]") @@ -107,7 +108,8 @@ compile [parse %actions as %body] to (..) elseif replacements[t[1]] then return replacements[t[1]] else - return t.type.."("..repr(tostring(t.source))..", "..repr(t[1].." \\0").."..string.format('%X', __MANGLE_INDEX))" + return t.type.."("..repr(tostring(t.source))..", "..repr(t[1].." \\0").."..string.format('%X', \ + ..__MANGLE_INDEX))" end end local \%new_body = LuaCode(\%body.source, @@ -230,4 +232,4 @@ compile [with local compile actions %body] to (..) action [Nomsu version]: use "lib/version.nom" return ".." - \(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version) + \(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version) \ No newline at end of file diff --git a/core/operators.nom b/core/operators.nom index eeb5e7a..70b0207 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains definitions of operators like "+" and "and". @@ -10,7 +10,7 @@ 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))") -compile [%a is %b, %a = %b, %a == %b] to (..) +compile [%a is %b, %a == %b] to (..) Lua value "(\(%a as lua expr) == \(%b as lua expr))" compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to (..) @@ -40,7 +40,7 @@ compile [% 's id, id of %] to (Lua value "IDS[\(% as lua expr)]") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Variable assignment operator -compile [%var <- %value] to: +compile [%var = %value] to: lua> "local \%var_lua = \(%var as lua)" assume %var_lua.is_value or barf "Invalid target for assignment: \%var" lua> ".." @@ -61,7 +61,7 @@ compile [%var <- %value] to: # Simultaneous mutli-assignments like: x,y,z = 1,x,3; -compile [<- %assignments, assign %assignments] to: +compile [set %assignments] to: assume (%assignments.type is "Dict") or barf ".." Expected a Dict for the assignments part of '<- %' statement, not \%assignments @@ -92,22 +92,22 @@ compile [<- %assignments, assign %assignments] to: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -compile [external %var <- %value] to: - %var_lua <- (%var as lua) +compile [external %var = %value] to: + %var_lua = (%var as lua) assume %var_lua.is_value or barf "Invalid target for assignment: \%var" - %value_lua <- (%value as lua) + %value_lua = (%value as lua) assume %value_lua.is_value or barf "Invalid value for assignment: \%value" return (Lua "\%var_lua = \%value_lua;") compile [with external %externs %body] to: - %body_lua <- (%body as lua statements) + %body_lua = (%body as lua statements) lua> ".." \%body_lua:remove_free_vars(table.map(\%externs, function(v) return tostring(nomsu:compile(v)) end)) return %body_lua compile [with %assignments %body] to: - %lua <- (%body as lua statements) + %lua = (%body as lua statements) lua> ".." local lhs, rhs = LuaCode(tree.source), LuaCode(tree.source) local vars = {} @@ -135,10 +135,7 @@ compile [with %assignments %body] to: \%lua:prepend("local ", lhs, " = ", rhs, ";\\n") return (..) - Lua ".." - do - \%lua - end -- 'with' block + Lua "do\n \%lua\nend -- 'with' block" # Math Operators @@ -194,17 +191,17 @@ compile [%x ARSHIFT %shift, %x >> %shift] to (..) # Unary operators compile [- %] to (Lua value "(- \(% as lua expr))") compile [not %] to (Lua value "(not \(% as lua expr))") -test: assume ((length of [1, 2, 3]) = 3) +test: assume ((length of [1, 2, 3]) == 3) compile [length of %list, || %list ||] to (Lua value "(#\(%list as lua expr))") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Update operators -parse [%var + <- %, %var +<- %] as (%var <- (%var + %)) -parse [%var - <- %, %var -<- %] as (%var <- (%var - %)) -parse [%var * <- %, %var *<- %] as (%var <- (%var * %)) -parse [%var / <- %, %var /<- %] as (%var <- (%var / %)) -parse [%var ^ <- %, %var ^<- %] as (%var <- (%var ^ %)) -parse [%var and<- %] as (%var <- (%var and %)) -parse [%var or<- %] as (%var <- (%var or %)) -parse [wrap %var around %] as (%var <- (%var wrapped around %)) +parse [%var += %] as (%var = (%var + %)) +parse [%var -= %] as (%var = (%var - %)) +parse [%var *= %] as (%var = (%var * %)) +parse [%var /= %] as (%var = (%var / %)) +parse [%var ^= %] as (%var = (%var ^ %)) +parse [%var and= %] as (%var = (%var and %)) +parse [%var or= %] as (%var = (%var or %)) +parse [wrap %var around %] as (%var = (%var wrapped around %)) diff --git a/core/scopes.nom b/core/scopes.nom index 256ddd5..2be24e6 100644 --- a/core/scopes.nom +++ b/core/scopes.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains definitions pertaining to variable scoping @@ -7,21 +7,18 @@ use "core/operators.nom" use "core/collections.nom" use "core/control_flow.nom" compile [with local %locals %body, with local %locals do %body] to: - %body_lua <- (%body as lua statements) + %body_lua = (%body as lua statements) when %locals.type = ?: * "Dict": - %body_lua <- (Lua "\(compile as (<- %locals))\n\%body_lua") + %body_lua = (..) + Lua "\(compile as (<- %locals))\n\%body_lua" + declare locals ("\(%.1 as lua)" for % in %locals) in %body_lua - * "List": - declare locals ("\(% as lua)" for % in %locals) in %body_lua + * "List": declare locals ("\(% as lua)" for % in %locals) in %body_lua * "Var" - * "Action": - declare locals ["\(%locals as lua)"] in %body_lua + * "Action": declare locals ["\(%locals as lua)"] in %body_lua *else (barf "Unexpected local: \(%locals as nomsu)") return (..) - Lua ".." - do - \%body_lua - end + Lua "do\n \%body_lua\nend" \ No newline at end of file diff --git a/core/text.nom b/core/text.nom index fa0d908..62ec056 100644 --- a/core/text.nom +++ b/core/text.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V2.2.4.3 +#!/usr/bin/env nomsu -V2.3.4.3 # This file contains some definitions of text escape sequences, including ANSI console color codes. @@ -66,4 +66,4 @@ lua> ".." return LuaCode.Value(tree.source, lua) end end - end + end \ No newline at end of file -- cgit v1.2.3