change table declaration syntax

This commit is contained in:
prod2 2022-02-08 08:56:47 +01:00
parent 8262ca187c
commit 5f3f61af2d
4 changed files with 20 additions and 12 deletions

View File

@ -31,9 +31,17 @@ tkStartList.genRule(parseList, nop, pcNone)
proc parseTable(comp: Compiler) = proc parseTable(comp: Compiler) =
var count: int var count: int
while comp.current.tokenType != tkRightBrace: while comp.current.tokenType notin {tkEof, tkRightBrace}:
comp.expression() if comp.match(tkLeftBracket):
comp.consume(tkEqual, "Equal sign expected after key.") 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() comp.expression()
count.inc() count.inc()
if comp.current.tokenType != tkRightBrace: if comp.current.tokenType != tkRightBrace:

View File

@ -56,8 +56,8 @@ var f2 = funct() {
var x = { @a @b var x = { @a @b
// this captures the internal value, not whatever it returns is assigned to // this captures the internal value, not whatever it returns is assigned to
:result = @{ :result = @{
"get" = funct() print (:a), get = funct() print (:a),
"set" = funct(n) :b = n, ["set"] = funct(n) :b = n,
}; };
}; };
x = 5; x = 5;
@ -85,9 +85,9 @@ argcap(8.1)();
var newAnimal = funct(species, color) { var newAnimal = funct(species, color) {
var species = species; // copy it so that it's mutable, if args ever get immutable var species = species; // copy it so that it's mutable, if args ever get immutable
var animal = @{ var animal = @{
"getSpecies" = funct() :result = species, ["getSpecies"] = funct() :result = species,
"setSpecies" = funct(newSpecies) species = newSpecies, ["setSpecies"] = funct(newSpecies) species = newSpecies,
"getColor" = funct() :result = color, // this captures an argument directly ["getColor"] = funct() :result = color, // this captures an argument directly
}; };
:result = animal; :result = animal;
}; };

View File

@ -59,7 +59,7 @@ if (1) print ("5");
if (@[]) print ("6"); if (@[]) print ("6");
if (@["hi"]) print ("7"); if (@["hi"]) print ("7");
if (@{}) print ("8"); if (@{}) print ("8");
if (@{"hi" = 5}) print ("9"); if (@{["hi"] = 5}) print ("9");
if (funct(n) print (n)) print ("10"); if (funct(n) print (n)) print ("10");
//expect:1 //expect:1

View File

@ -21,9 +21,9 @@ print (1 + 2 * 3 :: tostring() + "2");
// indexing tables with . // indexing tables with .
var nested = @{ var nested = @{
"lvl1" = @{ lvl1 = @{
"lvl2" = @{ lvl2 = @{
"lvl3" = 5 lvl3 = 5
} }
} }
}; };