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) =
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:

View File

@ -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;
};

View File

@ -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

View File

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