From 7251f750876fd7839e1d4c4a31e1448ec6ad2acb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 11 Jan 2018 15:32:54 -0800 Subject: [PATCH] Added "local %" macro as a stopgap for locals. --- lib/control_flow.nom | 10 +++++----- lib/operators.nom | 8 ++++++++ lib/text.nom | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/control_flow.nom b/lib/control_flow.nom index 05fb62d..fbc373f 100644 --- a/lib/control_flow.nom +++ b/lib/control_flow.nom @@ -84,7 +84,7 @@ immediately: for %var from %start to %stop by %step %body for %var from %start to %stop via %step %body ..to code: - lua> "local \%continue_labels, \%code, \%stop_labels" + local [%continue_labels, %code, %stop_labels] set %continue_labels = "" if (tree %body has function call \(do next for-loop)): %continue_labels join= "\n::continue_for::;" @@ -120,7 +120,7 @@ immediately: immediately: compile [for %var in %iterable %body] to code: - lua> "local \%continue_labels, \%stop_labels, \%code, \%stop_labels" + local [%continue_labels, %code, %stop_labels] set %continue_labels = "" if (tree %body has function call \(do next for-loop)): %continue_labels join= "\n::continue_for::;" @@ -150,7 +150,7 @@ immediately: compile [..] repeat %n times %body, repeat %n x %body ..to code: - lua> "local \%continue_labels, \%code, \%stop_labels" + local [%continue_labels, %code, %stop_labels] set %continue_labels = "" if (tree %body has function call \(do next repeat-loop)): %continue_labels join= "\n::continue_repeat::;" @@ -206,12 +206,12 @@ immediately: # Switch statement/multi-branch if immediately: compile [when %body] to code: - lua> "local \%result, \%fallthroughs, \%first" + local [%result, %fallthroughs, %first] set %result = "" set %fallthroughs = [] set %first = (yes) for %func_call in (%body's "value"): - lua> "local \%tokens, \%star, \%condition, \%action" + local [%tokens, %star, %condition, %action] assume ((%func_call's "type") == "FunctionCall") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. set %tokens = (%func_call's "value") diff --git a/lib/operators.nom b/lib/operators.nom index 7102a65..9f93a39 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -47,6 +47,14 @@ compile [%str |%start|] to: "\(%str as lua):sub(\(%start as lua), \(%start as lu compile [%str |%start - %stop|] to: "\(%str as lua):sub(\(%start as lua), \(%stop as lua))" # Variable assignment operator, and += type versions +compile [local %vars] to code: + lua> ".." + local locals = \%vars.type == "List" and \%vars.value or {\%vars}; + local identifiers = {}; + for i,x in ipairs(locals) do + identifiers[i] = nomsu:tree_to_lua(x).expr; + end + return "local "..table.concat(identifiers, ", "); compile [set %var = %val] to code: lua> ".." if \%var.type == 'List' and \%val.type == 'List' then diff --git a/lib/text.nom b/lib/text.nom index f7e4b00..7fa1a2c 100644 --- a/lib/text.nom +++ b/lib/text.nom @@ -2,7 +2,7 @@ use "lib/metaprogramming.nom" use "lib/control_flow.nom" use "lib/collections.nom" -lua> "local \%ansi, \%colors" +local [%ansi, %colors] set %ansi = {..} nl="\\n", newline="\\n", tab="\\t", bell="\\a", cr="\\r", "carriage return"="\\r" backspace="\\b", "form feed"="\\f", formfeed="\\f", "vertical tab"="\\v"