From 5f3f61af2d4c107cc9d4a535cf49f2cbd7cbd2ef Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Tue, 8 Feb 2022 08:56:47 +0100 Subject: [PATCH] change table declaration syntax --- src/ndspkg/compiler/collections.nim | 14 +++++++++++--- tests/closures.nds | 10 +++++----- tests/controlflow.nds | 2 +- tests/sugar.nds | 6 +++--- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/ndspkg/compiler/collections.nim b/src/ndspkg/compiler/collections.nim index e233fd8..9c76e9d 100644 --- a/src/ndspkg/compiler/collections.nim +++ b/src/ndspkg/compiler/collections.nim @@ -31,9 +31,17 @@ tkStartList.genRule(parseList, nop, pcNone) proc parseTable(comp: Compiler) = var count: int - while comp.current.tokenType != tkRightBrace: - comp.expression() - comp.consume(tkEqual, "Equal sign expected after key.") + while comp.current.tokenType notin {tkEof, tkRightBrace}: + if comp.match(tkLeftBracket): + comp.expression() + comp.consume(tkRightBracket, "Expect ']' after key.") + elif comp.match(tkIdentifier): + let ident = comp.previous.text.fromNimString() + comp.writeConstant(ident) + else: + comp.error("Key expected in table (perhaps you forgot to encapsulate the key with []?).") + if not comp.match(tkColon): + comp.consume(tkEqual, "Equal sign or colon expected after key.") comp.expression() count.inc() if comp.current.tokenType != tkRightBrace: diff --git a/tests/closures.nds b/tests/closures.nds index 37ea988..4380965 100644 --- a/tests/closures.nds +++ b/tests/closures.nds @@ -56,8 +56,8 @@ var f2 = funct() { var x = { @a @b // this captures the internal value, not whatever it returns is assigned to :result = @{ - "get" = funct() print (:a), - "set" = funct(n) :b = n, + get = funct() print (:a), + ["set"] = funct(n) :b = n, }; }; x = 5; @@ -85,9 +85,9 @@ argcap(8.1)(); var newAnimal = funct(species, color) { var species = species; // copy it so that it's mutable, if args ever get immutable var animal = @{ - "getSpecies" = funct() :result = species, - "setSpecies" = funct(newSpecies) species = newSpecies, - "getColor" = funct() :result = color, // this captures an argument directly + ["getSpecies"] = funct() :result = species, + ["setSpecies"] = funct(newSpecies) species = newSpecies, + ["getColor"] = funct() :result = color, // this captures an argument directly }; :result = animal; }; diff --git a/tests/controlflow.nds b/tests/controlflow.nds index 0055350..03c5c0b 100644 --- a/tests/controlflow.nds +++ b/tests/controlflow.nds @@ -59,7 +59,7 @@ if (1) print ("5"); if (@[]) print ("6"); if (@["hi"]) print ("7"); if (@{}) print ("8"); -if (@{"hi" = 5}) print ("9"); +if (@{["hi"] = 5}) print ("9"); if (funct(n) print (n)) print ("10"); //expect:1 diff --git a/tests/sugar.nds b/tests/sugar.nds index 06d778d..1c79729 100644 --- a/tests/sugar.nds +++ b/tests/sugar.nds @@ -21,9 +21,9 @@ print (1 + 2 * 3 :: tostring() + "2"); // indexing tables with . var nested = @{ - "lvl1" = @{ - "lvl2" = @{ - "lvl3" = 5 + lvl1 = @{ + lvl2 = @{ + lvl3 = 5 } } };