diff options
Diffstat (limited to 'alv')
| -rw-r--r-- | alv/ast.moon | 4 | ||||
| -rw-r--r-- | alv/builtins.moon | 14 | ||||
| -rw-r--r-- | alv/cell.moon | 34 | ||||
| -rw-r--r-- | alv/parsing.moon | 22 |
4 files changed, 52 insertions, 22 deletions
diff --git a/alv/ast.moon b/alv/ast.moon index 4d0ecae..52117c6 100644 --- a/alv/ast.moon +++ b/alv/ast.moon @@ -32,7 +32,7 @@ -- @function stringify -- @treturn string the exact string this Node was parsed from -import Cell, RootCell from require 'alv.cell' +import Cell, RootCell, ArrayCell, StructCell from require 'alv.cell' import TemplateString from require 'alv.template_string' import Constant from require 'alv.result.const' import Dummy from require 'alv.dummy' @@ -41,6 +41,8 @@ import Tag from require 'alv.tag' { :Cell :RootCell + :ArrayCell + :StructCell :TemplateString :Constant :Dummy diff --git a/alv/builtins.moon b/alv/builtins.moon index d76effc..0d188f9 100644 --- a/alv/builtins.moon +++ b/alv/builtins.moon @@ -539,11 +539,11 @@ In case of collisions, the event that comes first in the argument list wins." @out\set input! return -array = Constant.meta +mkarray = Constant.meta meta: - name: 'array' + name: 'mkarray' summary: "Construct an array." - examples: { '(array a b c…)' } + examples: { '[a b c…]', '(mkarray a b c…)' } description: "Produces an array of values. `a`, `b`, `c`… have to be values of the same type. @@ -559,11 +559,11 @@ This is a pure op, so at most one !-stream input is allowed." args = @unwrap_all! @out\set args -struct = Constant.meta +mkstruct = Constant.meta meta: - name: 'struct' + name: 'mkstruct' summary: "Construct an struct." - examples: { '(struct key1 val1 [key2 val2…])' } + examples: { '{key1 val1 [key2 val2…]}', '(mkstruct key1 val1 [key2 val2…])' } description: "Produces a struct of values. `key1`, `key2`, … have to be constant expressions. @@ -732,7 +732,7 @@ Constant.meta '->': thread_first '->>': thread_last - :array, :struct + :mkarray, :mkstruct :loop, :recur diff --git a/alv/cell.moon b/alv/cell.moon index 7b6cf61..7f2c027 100644 --- a/alv/cell.moon +++ b/alv/cell.moon @@ -155,6 +155,10 @@ class RootCell extends Cell head: => Constant.sym 'do' tail: => @children + new: (...) => + super ... + @tag = Tag.parse '0' + stringify: => buf = '' buf ..= @white[0] @@ -172,12 +176,34 @@ class RootCell extends Cell -- -- @tparam table parts -- @treturn Cell - @parse: (...) -> - tag, children, white = parse_args (Tag.parse '0'), ... - RootCell tag, children, white + @parse: (parts) => + _, children, white = parse_args nil, parts + @@ nil, children, white + +-- @type ArrayCell +class ArrayCell extends RootCell + head: => Constant.sym 'mkarray' + tail: => @children + stringify: => '[' .. super! .. ']' + + new: (...) => + Cell.__init @, ... + assert #@children > 0, Error 'syntax', "array literal can't be empty" + +-- @type StructCell +class StructCell extends RootCell + head: => Constant.sym 'mkstruct' + tail: => @children + stringify: => '{' .. super! .. '}' + + new: (...) => + Cell.__init @, ... + assert #@children > 0, Error 'syntax', "struct literal can't be empty" + assert #@children % 2 == 0, Error 'syntax', "struct literal can't have uneven number values" { :Cell :RootCell - :TemplateString + :ArrayCell + :StructCell } diff --git a/alv/parsing.moon b/alv/parsing.moon index d5ec313..e8cc5b6 100644 --- a/alv/parsing.moon +++ b/alv/parsing.moon @@ -2,7 +2,7 @@ -- Lpeg Grammar for parsing `alive` code. -- -- @module parsing -import Cell, RootCell, Constant, TemplateString, Tag from require 'alv.ast' +import Cell, RootCell, ArrayCell, StructCell, Constant, TemplateString, Tag from require 'alv.ast' import R, S, P, V, C, Ct from require 'lpeg' -- whitespace @@ -29,26 +29,28 @@ fract = digit^1 * '/' * digit^1 float = (digit^1 * '.' * digit^0) + (digit^0 * '.' * digit^1) num = ((P '-')^-1 * (float + fract + int)) / Constant\parse 'num' -atom = num + sym + str -expr = (V 'cell') + atom - tplcont = ((P '\\"') + (P '\\\\') + (1 - (P '"') - (P '#')))^1 / 1 -tplstr = (P '#"') * ((P '##') / 1 + ('#' * expr) + tplcont)^0 * '"' / TemplateString\parse +tplstr = (P '#"') * ((P '##') / 1 + ('#' * (V 'expr')) + tplcont)^0 * '"' / TemplateString\parse -expitem = tplstr + expr +expitem = tplstr + (V 'expr') explist = Ct mspace * (expitem * (space * expitem)^0 * mspace)^-1 tag = (P '[') * (digit^1 / Tag.parse) * (P ']') cell = (P '(') * tag^-1 * explist * (P ')') / Cell.parse +array = (P '[') * explist * (P ']') / ArrayCell\parse +struct = (P '{') * explist * (P '}') / StructCell\parse + +atom = num + sym + str +expr = cell + array + struct + atom root = P { - explist / RootCell.parse - :cell + explist / RootCell\parse + :expr } cell = P { - 'cell' - :cell + cell + :expr } program = root * -1 |
