Change table syntax to {key:value} instead of {key=>value}
This commit is contained in:
parent
cc07637134
commit
63e6ba596a
@ -510,7 +510,7 @@ public CORD Table$as_text(const table_t *t, bool colorize, const TypeInfo *type)
|
||||
auto table = type->TableInfo;
|
||||
|
||||
if (!t)
|
||||
return CORD_all("{", generic_as_text(NULL, false, table.key), "=>", generic_as_text(NULL, false, table.value), "}");
|
||||
return CORD_all("{", generic_as_text(NULL, false, table.key), ":", generic_as_text(NULL, false, table.value), "}");
|
||||
|
||||
int64_t val_off = value_offset(type);
|
||||
CORD c = "{";
|
||||
@ -519,7 +519,7 @@ public CORD Table$as_text(const table_t *t, bool colorize, const TypeInfo *type)
|
||||
c = CORD_cat(c, ", ");
|
||||
void *entry = GET_ENTRY(*t, i);
|
||||
c = CORD_cat(c, generic_as_text(entry, colorize, table.key));
|
||||
c = CORD_cat(c, "=>");
|
||||
c = CORD_cat(c, ":");
|
||||
c = CORD_cat(c, generic_as_text(entry + val_off, colorize, table.value));
|
||||
}
|
||||
|
||||
|
8
parse.c
8
parse.c
@ -466,7 +466,7 @@ type_ast_t *parse_table_type(parse_ctx_t *ctx, const char *pos) {
|
||||
if (!key_type) return NULL;
|
||||
pos = key_type->end;
|
||||
whitespace(&pos);
|
||||
if (!match(&pos, "=>")) return NULL;
|
||||
if (!match(&pos, ":")) return NULL;
|
||||
type_ast_t *value_type = expect(ctx, start, &pos, parse_type, "I couldn't parse the rest of this table type");
|
||||
whitespace(&pos);
|
||||
expect_closing(ctx, &pos, "}", "I wasn't able to parse the rest of this table type");
|
||||
@ -658,8 +658,8 @@ PARSER(parse_table) {
|
||||
whitespace(&pos);
|
||||
key_type = expect(ctx, pos-1, &pos, parse_type, "I couldn't parse a key type for this table");
|
||||
whitespace(&pos);
|
||||
if (!match(&pos, "=>"))
|
||||
parser_err(ctx, pos, pos, "I expected an '=>' for this table type");
|
||||
if (!match(&pos, ":"))
|
||||
parser_err(ctx, pos, pos, "I expected an ':' for this table type");
|
||||
value_type = expect(ctx, pos-1, &pos, parse_type, "I couldn't parse a value type for this table");
|
||||
whitespace(&pos);
|
||||
}
|
||||
@ -669,7 +669,7 @@ PARSER(parse_table) {
|
||||
ast_t *key = optional(ctx, &pos, parse_extended_expr);
|
||||
if (!key) break;
|
||||
whitespace(&pos);
|
||||
if (!match(&pos, "=>")) return NULL;
|
||||
if (!match(&pos, ":")) return NULL;
|
||||
ast_t *value = expect(ctx, pos-1, &pos, parse_expr, "I couldn't parse the value for this table entry");
|
||||
ast_t *entry = NewAST(ctx->file, entry_start, pos, TableEntry, .key=key, .value=value);
|
||||
ast_t *suffixed = parse_comprehension_suffix(ctx, entry);
|
||||
|
@ -21,7 +21,7 @@ func main()
|
||||
= yes
|
||||
|
||||
>> x := Foo.One(123)
|
||||
>> t := {x=>"found"; default="missing"}
|
||||
>> t := {x:"found"; default="missing"}
|
||||
>> t[x]
|
||||
= "found"
|
||||
>> t[Foo.Zero]
|
||||
|
12
test/for.tm
12
test/for.tm
@ -15,14 +15,14 @@ func labeled_nums(nums:[Int])->Text
|
||||
return "EMPTY"
|
||||
return result
|
||||
|
||||
func table_str(t:{Text=>Text})->Text
|
||||
func table_str(t:{Text:Text})->Text
|
||||
str := ""
|
||||
for k,v in t
|
||||
str ++= "{k}=>{v},"
|
||||
str ++= "{k}:{v},"
|
||||
else return "EMPTY"
|
||||
return str
|
||||
|
||||
func table_key_str(t:{Text=>Text})->Text
|
||||
func table_key_str(t:{Text:Text})->Text
|
||||
str := ""
|
||||
for k in t
|
||||
str ++= "{k},"
|
||||
@ -40,10 +40,10 @@ func main()
|
||||
>> labeled_nums([:Int])
|
||||
= "EMPTY"
|
||||
|
||||
>> t := {"key1"=>"value1", "key2"=>"value2"}
|
||||
>> t := {"key1":"value1", "key2":"value2"}
|
||||
>> table_str(t)
|
||||
= "key1=>value1,key2=>value2,"
|
||||
>> table_str({:Text=>Text})
|
||||
= "key1:value1,key2:value2,"
|
||||
>> table_str({:Text:Text})
|
||||
= "EMPTY"
|
||||
|
||||
>> table_key_str(t)
|
||||
|
@ -26,7 +26,7 @@ func test_metamethods()
|
||||
|
||||
>> x < Pair(11, 20)
|
||||
= yes
|
||||
>> t2 := {x=> "found"; default="missing"}
|
||||
>> t2 := {x:"found"; default="missing"}
|
||||
>> t2[x]
|
||||
= "found"
|
||||
>> t2[y]
|
||||
@ -43,7 +43,7 @@ func test_mixed()
|
||||
= no
|
||||
>> x < Mixed(11, "Hello")
|
||||
= yes
|
||||
>> t := {x=> "found"; default="missing"}
|
||||
>> t := {x:"found"; default="missing"}
|
||||
>> t[x]
|
||||
= "found"
|
||||
>> t[y]
|
||||
@ -58,8 +58,8 @@ func main()
|
||||
|
||||
>> my_pass := Password("Swordfish")
|
||||
= Password(...)
|
||||
>> users_by_password := {my_pass=> "User1", Password("xxx")=>"User2"}
|
||||
= {Password(...)=>"User1", Password(...)=>"User2"}
|
||||
>> users_by_password := {my_pass:"User1", Password("xxx"):"User2"}
|
||||
= {Password(...):"User1", Password(...):"User2"}
|
||||
>> users_by_password[my_pass]
|
||||
= "User1"
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
func main()
|
||||
>> t := {"one"=>1, "two"=>2; default=999}
|
||||
= {"one"=>1, "two"=>2; default=999}
|
||||
>> t := {"one":1, "two":2; default=999}
|
||||
= {"one":1, "two":2; default=999}
|
||||
|
||||
>> t["one"]
|
||||
= 1
|
||||
@ -11,24 +11,24 @@ func main()
|
||||
|
||||
t_str := ""
|
||||
for k,v in t
|
||||
t_str ++= "({k}=>{v})"
|
||||
t_str ++= "({k}:{v})"
|
||||
>> t_str
|
||||
= "(one=>1)(two=>2)"
|
||||
= "(one:1)(two:2)"
|
||||
|
||||
>> #t
|
||||
= 2
|
||||
>> t.default
|
||||
= ?%999
|
||||
>> t.fallback
|
||||
= !{Text=>Int}
|
||||
= !{Text:Int}
|
||||
|
||||
>> t.keys
|
||||
= ["one", "two"]
|
||||
>> t.values
|
||||
= [1, 2]
|
||||
|
||||
>> t2 := {"three"=>3; fallback=t}
|
||||
= {"three"=>3; fallback={"one"=>1, "two"=>2; default=999}}
|
||||
>> t2 := {"three":3; fallback=t}
|
||||
= {"three":3; fallback={"one":1, "two":2; default=999}}
|
||||
|
||||
>> t2["one"]
|
||||
= 1
|
||||
@ -42,17 +42,17 @@ func main()
|
||||
>> t2.default
|
||||
= !Int
|
||||
>> t2.fallback
|
||||
= ?%{"one"=>1, "two"=>2; default=999}
|
||||
= ?%{"one":1, "two":2; default=999}
|
||||
|
||||
t2_str := ""
|
||||
for k,v in t2
|
||||
t2_str ++= "({k}=>{v})"
|
||||
t2_str ++= "({k}:{v})"
|
||||
>> t2_str
|
||||
= "(three=>3)"
|
||||
= "(three:3)"
|
||||
|
||||
>> {i=>10*i for i in 5}
|
||||
= {1=>10, 2=>20, 3=>30, 4=>40, 5=>50}
|
||||
>> {i=>10*i for i in 5 if i mod 2 != 0}
|
||||
= {1=>10, 3=>30, 5=>50}
|
||||
>> {x=>10*x for x in y if x > 1 for y in [3, 4, 5] if y < 5}
|
||||
= {2=>20, 3=>30, 4=>40}
|
||||
>> {i:10*i for i in 5}
|
||||
= {1:10, 2:20, 3:30, 4:40, 5:50}
|
||||
>> {i:10*i for i in 5 if i mod 2 != 0}
|
||||
= {1:10, 3:30, 5:50}
|
||||
>> {x:10*x for x in y if x > 1 for y in [3, 4, 5] if y < 5}
|
||||
= {2:20, 3:30, 4:40}
|
||||
|
2
types.c
2
types.c
@ -25,7 +25,7 @@ CORD type_to_cord(type_t *t) {
|
||||
}
|
||||
case TableType: {
|
||||
auto table = Match(t, TableType);
|
||||
return CORD_asprintf("{%r=>%r}", type_to_cord(table->key_type), type_to_cord(table->value_type));
|
||||
return CORD_asprintf("{%r:%r}", type_to_cord(table->key_type), type_to_cord(table->value_type));
|
||||
}
|
||||
case ClosureType: {
|
||||
return type_to_cord(Match(t, ClosureType)->fn);
|
||||
|
Loading…
Reference in New Issue
Block a user