Skip to content

Commit

Permalink
core!: Rename Map type do Dict and implements map, forEach and reduce…
Browse files Browse the repository at this point in the history
… builtin functions
  • Loading branch information
moisespsena committed Nov 13, 2023
2 parents 167433a + c89a618 commit 153bee2
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (c *Compiler) Compile(nd ast.Node) error {
return c.compileIdent(nt)
case *node.ArrayLit:
return c.compileArrayLit(nt)
case *node.MapLit:
case *node.DictLit:
return c.compileMapLit(nt)
case *node.KeyValueArrayLit:
return c.compileKeyValueArrayLit(nt)
Expand Down
2 changes: 1 addition & 1 deletion compiler_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ func (c *Compiler) compileArrayLit(nd *node.ArrayLit) error {
return nil
}

func (c *Compiler) compileMapLit(nd *node.MapLit) error {
func (c *Compiler) compileMapLit(nd *node.DictLit) error {
for _, elt := range nd.Elements {
// key
c.emit(nd, OpConstant, c.addConstant(String(elt.Key)))
Expand Down
4 changes: 2 additions & 2 deletions objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestObjectTypeName(t *testing.T) {
require.Equal(t, "string", String("").Type().Name())
require.Equal(t, "array", Array{}.Type().Name())
require.Equal(t, "bytes", Bytes{}.Type().Name())
require.Equal(t, "map", Dict{}.Type().Name())
require.Equal(t, "syncMap", (&SyncMap{}).Type().Name())
require.Equal(t, "dict", Dict{}.Type().Name())
require.Equal(t, "syncDict", (&SyncMap{}).Type().Name())
require.Equal(t, "function", (&Function{}).Type().Name())
require.Equal(t, "builtinFunction", (&BuiltinFunction{}).Type().Name())
require.Equal(t, "compiledFunction", (&CompiledFunction{}).Type().Name())
Expand Down
2 changes: 1 addition & 1 deletion optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (so *SimpleOptimizer) optimize(nd ast.Node) (node.Expr, bool) {
nd.Elements[i] = expr
}
}
case *node.MapLit:
case *node.DictLit:
for i := range nd.Elements {
if expr, ok = so.optimize(nd.Elements[i].Value); ok {
nd.Elements[i].Value = expr
Expand Down
26 changes: 13 additions & 13 deletions parser/node/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,50 +630,50 @@ func (e *UintLit) String() string {
return e.Literal
}

// MapElementLit represents a map element.
type MapElementLit struct {
// DictElementLit represents a map element.
type DictElementLit struct {
Key string
KeyPos source.Pos
ColonPos source.Pos
Value Expr
}

func (e *MapElementLit) ExprNode() {}
func (e *DictElementLit) ExprNode() {}

// Pos returns the position of first character belonging to the node.
func (e *MapElementLit) Pos() source.Pos {
func (e *DictElementLit) Pos() source.Pos {
return e.KeyPos
}

// End returns the position of first character immediately after the node.
func (e *MapElementLit) End() source.Pos {
func (e *DictElementLit) End() source.Pos {
return e.Value.End()
}

func (e *MapElementLit) String() string {
func (e *DictElementLit) String() string {
return e.Key + ": " + e.Value.String()
}

// MapLit represents a map literal.
type MapLit struct {
// DictLit represents a map literal.
type DictLit struct {
LBrace source.Pos
Elements []*MapElementLit
Elements []*DictElementLit
RBrace source.Pos
}

func (e *MapLit) ExprNode() {}
func (e *DictLit) ExprNode() {}

// Pos returns the position of first character belonging to the node.
func (e *MapLit) Pos() source.Pos {
func (e *DictLit) Pos() source.Pos {
return e.LBrace
}

// End returns the position of first character immediately after the node.
func (e *MapLit) End() source.Pos {
func (e *DictLit) End() source.Pos {
return e.RBrace + 1
}

func (e *MapLit) String() string {
func (e *DictLit) String() string {
var elements []string
for _, m := range e.Elements {
elements = append(elements, m.String())
Expand Down
22 changes: 11 additions & 11 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (p *Parser) ParseCallArgs(tlparen, trparen token.Token) *node.CallArgs {
elipsis := &node.EllipsisValue{Pos: p.Token.Pos}
p.Next()
elipsis.Value = p.ParseExpr()
if _, ok := elipsis.Value.(*node.MapLit); ok {
if _, ok := elipsis.Value.(*node.DictLit); ok {
namedArgs.Ellipsis = elipsis
goto done
} else {
Expand Down Expand Up @@ -510,7 +510,7 @@ kw:
namedArgs.Names = append(namedArgs.Names, node.NamedArgExpr{Ident: t})
case *node.StringLit:
namedArgs.Names = append(namedArgs.Names, node.NamedArgExpr{Lit: t})
case *node.CallExpr, *node.SelectorExpr, *node.MapLit:
case *node.CallExpr, *node.SelectorExpr, *node.DictLit:
namedArgs.Ellipsis = &node.EllipsisValue{Pos: p.Token.Pos, Value: t}
p.Expect(token.Ellipsis)
if !p.AtComma("call argument", trparen) {
Expand Down Expand Up @@ -864,8 +864,8 @@ func (p *Parser) ParseOperand() node.Expr {
return p.ParseParemExpr()
case token.LBrack: // array literal
return p.ParseArrayLit()
case token.LBrace: // map literal
return p.ParseMapLit()
case token.LBrace: // dict literal
return p.ParseDictLit()
case token.Func: // function literal
return p.ParseFuncLit()
case token.Text:
Expand Down Expand Up @@ -2097,9 +2097,9 @@ func (p *Parser) ParseExprList() (list []node.Expr) {
return
}

func (p *Parser) ParseMapElementLit() *node.MapElementLit {
func (p *Parser) ParseMapElementLit() *node.DictElementLit {
if p.Trace {
defer untracep(tracep(p, "MapElementLit"))
defer untracep(tracep(p, "DictElementLit"))
}

pos := p.Token.Pos
Expand All @@ -2115,23 +2115,23 @@ func (p *Parser) ParseMapElementLit() *node.MapElementLit {
p.Next()
colonPos := p.Expect(token.Colon)
valueExpr := p.ParseExpr()
return &node.MapElementLit{
return &node.DictElementLit{
Key: name,
KeyPos: pos,
ColonPos: colonPos,
Value: valueExpr,
}
}

func (p *Parser) ParseMapLit() *node.MapLit {
func (p *Parser) ParseDictLit() *node.DictLit {
if p.Trace {
defer untracep(tracep(p, "MapLit"))
defer untracep(tracep(p, "DictLit"))
}

lbrace := p.Expect(token.LBrace)
p.ExprLevel++

var elements []*node.MapElementLit
var elements []*node.DictElementLit
for p.Token.Token != token.RBrace && p.Token.Token != token.EOF {
elements = append(elements, p.ParseMapElementLit())

Expand All @@ -2143,7 +2143,7 @@ func (p *Parser) ParseMapLit() *node.MapLit {

p.ExprLevel--
rbrace := p.Expect(token.RBrace)
return &node.MapLit{
return &node.DictLit{
LBrace: lbrace,
RBrace: rbrace,
Elements: elements,
Expand Down
48 changes: 24 additions & 24 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ func TestParseCallWithNamedArgs(t *testing.T) {
callExpr(
ident("add", p(1, 1)),
p(1, 4), p(1, 15),
callExprNamedArgs(ellipsis(Pos(12), mapLit(10, 11)),
callExprNamedArgs(ellipsis(Pos(12), dictLit(10, 11)),
[]NamedArgExpr{{Ident: ident("x", p(1, 6))}},
[]Expr{intLit(2, p(1, 8))},
))))
Expand Down Expand Up @@ -1480,7 +1480,7 @@ func TestParseForIn(t *testing.T) {
forInStmt(
ident("x", p(1, 5)),
ident("y", p(1, 8)),
mapLit(
dictLit(
p(1, 13), p(1, 26),
mapElementLit(
"k1", p(1, 14), p(1, 16), intLit(1, p(1, 18))),
Expand Down Expand Up @@ -2235,7 +2235,7 @@ func TestParseIndex(t *testing.T) {
return stmts(
exprStmt(
indexExpr(
mapLit(p(1, 1), p(1, 12),
dictLit(p(1, 1), p(1, 12),
mapElementLit(
"a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
mapElementLit(
Expand All @@ -2248,7 +2248,7 @@ func TestParseIndex(t *testing.T) {
return stmts(
exprStmt(
indexExpr(
mapLit(p(1, 1), p(1, 12),
dictLit(p(1, 1), p(1, 12),
mapElementLit(
"a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
mapElementLit(
Expand Down Expand Up @@ -2312,7 +2312,7 @@ func TestParseMap(t *testing.T) {
expectParse(t, "{ key1: 1, key2: \"2\", key3: true }", func(p pfn) []Stmt {
return stmts(
exprStmt(
mapLit(p(1, 1), p(1, 34),
dictLit(p(1, 1), p(1, 34),
mapElementLit(
"key1", p(1, 3), p(1, 7), intLit(1, p(1, 9))),
mapElementLit(
Expand All @@ -2324,7 +2324,7 @@ func TestParseMap(t *testing.T) {
expectParse(t, "{ \"key1\": 1 }", func(p pfn) []Stmt {
return stmts(
exprStmt(
mapLit(p(1, 1), p(1, 13),
dictLit(p(1, 1), p(1, 13),
mapElementLit(
"key1", p(1, 3), p(1, 9), intLit(1, p(1, 11))))))
})
Expand All @@ -2333,7 +2333,7 @@ func TestParseMap(t *testing.T) {
func(p pfn) []Stmt {
return stmts(assignStmt(
exprs(ident("a", p(1, 1))),
exprs(mapLit(p(1, 5), p(1, 38),
exprs(dictLit(p(1, 5), p(1, 38),
mapElementLit(
"key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
mapElementLit(
Expand All @@ -2348,14 +2348,14 @@ func TestParseMap(t *testing.T) {
func(p pfn) []Stmt {
return stmts(assignStmt(
exprs(ident("a", p(1, 1))),
exprs(mapLit(p(1, 5), p(1, 54),
exprs(dictLit(p(1, 5), p(1, 54),
mapElementLit(
"key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
mapElementLit(
"key2", p(1, 16), p(1, 20), stringLit("2", p(1, 22))),
mapElementLit(
"key3", p(1, 27), p(1, 31),
mapLit(p(1, 33), p(1, 52),
dictLit(p(1, 33), p(1, 52),
mapElementLit(
"k1", p(1, 35),
p(1, 37), stringLit("bar", p(1, 39))),
Expand All @@ -2373,7 +2373,7 @@ func TestParseMap(t *testing.T) {
key3: true,
}`, func(p pfn) []Stmt {
return stmts(exprStmt(
mapLit(p(2, 1), p(6, 1),
dictLit(p(2, 1), p(6, 1),
mapElementLit(
"key1", p(3, 2), p(3, 6), intLit(1, p(3, 8))),
mapElementLit(
Expand Down Expand Up @@ -2513,7 +2513,7 @@ func TestParseSelector(t *testing.T) {
return stmts(
exprStmt(
selectorExpr(
mapLit(
dictLit(
p(1, 1), p(1, 6),
mapElementLit(
"k1", p(1, 2), p(1, 4), intLit(1, p(1, 5)))),
Expand All @@ -2525,10 +2525,10 @@ func TestParseSelector(t *testing.T) {
exprStmt(
selectorExpr(
selectorExpr(
mapLit(
dictLit(
p(1, 1), p(1, 11),
mapElementLit("k1", p(1, 2), p(1, 4),
mapLit(p(1, 5), p(1, 10),
dictLit(p(1, 5), p(1, 10),
mapElementLit(
"v1", p(1, 6),
p(1, 8), intLit(1, p(1, 9)))))),
Expand Down Expand Up @@ -3248,17 +3248,17 @@ func mapElementLit(
keyPos Pos,
colonPos Pos,
value Expr,
) *MapElementLit {
return &MapElementLit{
) *DictElementLit {
return &DictElementLit{
Key: key, KeyPos: keyPos, ColonPos: colonPos, Value: value,
}
}

func mapLit(
func dictLit(
lbrace, rbrace Pos,
list ...*MapElementLit,
) *MapLit {
return &MapLit{LBrace: lbrace, RBrace: rbrace, Elements: list}
list ...*DictElementLit,
) *DictLit {
return &DictLit{LBrace: lbrace, RBrace: rbrace, Elements: list}
}

func funcLit(funcType *FuncType, body *BlockStmt) *FuncLit {
Expand Down Expand Up @@ -3548,13 +3548,13 @@ func equalExpr(t *testing.T, expected, actual Expr) {
actual.(*ArrayLit).RBrack)
equalExprs(t, expected.Elements,
actual.(*ArrayLit).Elements)
case *MapLit:
case *DictLit:
require.Equal(t, expected.LBrace,
actual.(*MapLit).LBrace)
actual.(*DictLit).LBrace)
require.Equal(t, expected.RBrace,
actual.(*MapLit).RBrace)
actual.(*DictLit).RBrace)
equalMapElements(t, expected.Elements,
actual.(*MapLit).Elements)
actual.(*DictLit).Elements)
case *NilLit:
require.Equal(t, expected.TokenPos,
actual.(*NilLit).TokenPos)
Expand Down Expand Up @@ -3762,7 +3762,7 @@ func equalStmts(t *testing.T, expected, actual []Stmt) {

func equalMapElements(
t *testing.T,
expected, actual []*MapElementLit,
expected, actual []*DictElementLit,
) {
require.Equal(t, len(expected), len(actual))
for i := 0; i < len(expected); i++ {
Expand Down
6 changes: 3 additions & 3 deletions parser/testdata/trace.golden
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,9 @@
293: 29: 12: . . . . . . . UnaryExpression (
293: 29: 12: . . . . . . . . PrimaryExpression (
293: 29: 12: . . . . . . . . . Operand (
293: 29: 12: . . . . . . . . . . MapLit (
293: 29: 12: . . . . . . . . . . DictLit (
293: 29: 12: . . . . . . . . . . . "{"
294: 29: 13: . . . . . . . . . . . MapElementLit (
294: 29: 13: . . . . . . . . . . . DictElementLit (
294: 29: 13: . . . . . . . . . . . . IDENT a
295: 29: 14: . . . . . . . . . . . . ":"
297: 29: 16: . . . . . . . . . . . . Expression (
Expand All @@ -553,7 +553,7 @@
298: 29: 17: . . . . . . . . . . . . )
298: 29: 17: . . . . . . . . . . . )
298: 29: 17: . . . . . . . . . . . ","
300: 29: 19: . . . . . . . . . . . MapElementLit (
300: 29: 19: . . . . . . . . . . . DictElementLit (
300: 29: 19: . . . . . . . . . . . . IDENT b
301: 29: 20: . . . . . . . . . . . . ":"
303: 29: 22: . . . . . . . . . . . . Expression (
Expand Down
4 changes: 2 additions & 2 deletions stdlib/json/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ func TestCycle(t *testing.T) {
expectRun(t, `json:=import("json");a:=[1,2];a[1]=a;return string(json.MarshalIndent(a,""," "))`,
nil, String(`error: json: unsupported value: encountered a cycle via array`))
expectRun(t, `json:=import("json");m:={a:1};m.b=m;return string(json.Marshal(m))`,
nil, String(`error: json: unsupported value: encountered a cycle via map`))
nil, String(`error: json: unsupported value: encountered a cycle via dict`))
expectRun(t, `param m;json:=import("json");m.b=m;return string(json.Marshal(m))`,
newOpts().Args(&SyncMap{Value: Dict{}}),
String(`error: json: unsupported value: encountered a cycle via syncMap`))
String(`error: json: unsupported value: encountered a cycle via syncDict`))

ptr := &ObjectPtr{}
var m Object = Dict{}
Expand Down
2 changes: 1 addition & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ func TestVMBuiltinFunction(t *testing.T) {
expectRun(t, `return typeName('a')`, nil, String("char"))
expectRun(t, `return typeName("")`, nil, String("string"))
expectRun(t, `return typeName([])`, nil, String("array"))
expectRun(t, `return typeName({})`, nil, String("map"))
expectRun(t, `return typeName({})`, nil, String("dict"))
expectRun(t, `return typeName(error(""))`, nil, String("error"))
expectRun(t, `return typeName(bytes())`, nil, String("bytes"))
expectRun(t, `return typeName(func(){})`, nil, String("compiledFunction"))
Expand Down

0 comments on commit 153bee2

Please sign in to comment.