-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove pull_request and master push build * init * Add map parser * init map evaluator * will merge * add hashmap examples * Update README.md
- Loading branch information
Showing
18 changed files
with
644 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,8 @@ name: build | |
|
||
on: | ||
push: | ||
branches: | ||
- "mbuye" | ||
tags: | ||
- "v*" | ||
pull_request: | ||
|
||
jobs: | ||
goreleaser: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mgwirizano munthu = { | ||
"dzina": "Soma", | ||
"zaka": 3 | ||
} | ||
|
||
lembanzr(munthu["dzina"]) | ||
lembanzr(munthu["zaka"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ast | ||
|
||
import ( | ||
"github.com/sevenreup/chewa/src/token" | ||
) | ||
|
||
type MapExpression struct { | ||
Expression | ||
Token token.Token | ||
Pairs map[Expression]Expression | ||
} | ||
|
||
func (ie *MapExpression) TokenLiteral() string { return ie.Token.Literal } | ||
|
||
// TODO: Print this one properly | ||
func (ie *MapExpression) String() string { | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/sevenreup/chewa/src/ast" | ||
"github.com/sevenreup/chewa/src/object" | ||
) | ||
|
||
func evalMapExpression(node *ast.MapExpression, | ||
env *object.Environment) object.Object { | ||
pairs := make(map[object.MapKey]object.MapPair) | ||
|
||
for keyNode, valueNode := range node.Pairs { | ||
identifier, ok := keyNode.(*ast.Identifier) | ||
|
||
if ok { | ||
keyNode = &ast.StringLiteral{ | ||
Token: identifier.Token, | ||
Value: identifier.Value, | ||
} | ||
} | ||
|
||
key := Eval(keyNode, env) | ||
|
||
if isError(key) { | ||
return key | ||
} | ||
|
||
mapKey, ok := key.(object.Mappable) | ||
|
||
if !ok { | ||
return newError("%d:%d:%s: runtime error: unusable as map key: %s", node.Token.Pos.Line, node.Token.Pos.Column, node.Token.File, key.Type()) | ||
} | ||
|
||
value := Eval(valueNode, env) | ||
|
||
if isError(value) { | ||
return value | ||
} | ||
|
||
hashed := mapKey.MapKey() | ||
|
||
pairs[hashed] = object.MapPair{Key: key, Value: value} | ||
} | ||
|
||
return &object.Map{Pairs: pairs} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package object | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const MAP_OBJ = "MAP" | ||
|
||
type Map struct { | ||
Pairs map[MapKey]MapPair | ||
} | ||
|
||
type MapPair struct { | ||
Key Object | ||
Value Object | ||
} | ||
|
||
func (m *Map) Type() ObjectType { return MAP_OBJ } | ||
|
||
func (m *Map) Inspect() string { | ||
var out bytes.Buffer | ||
pairs := []string{} | ||
for _, pair := range m.Pairs { | ||
pairs = append(pairs, fmt.Sprintf("%s: %s", | ||
pair.Key.Inspect(), pair.Value.Inspect())) | ||
} | ||
out.WriteString("{") | ||
out.WriteString(strings.Join(pairs, ", ")) | ||
out.WriteString("}") | ||
return out.String() | ||
} | ||
|
||
func (m *Map) Method(method string, args []Object) (Object, bool) { | ||
//TODO implement me | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package object | ||
|
||
import "testing" | ||
|
||
func TestStringMapKey(t *testing.T) { | ||
hello1 := &String{Value: "Hello World"} | ||
hello2 := &String{Value: "Hello World"} | ||
diff1 := &String{Value: "My name is johnny"} | ||
diff2 := &String{Value: "My name is johnny"} | ||
if hello1.MapKey() != hello2.MapKey() { | ||
t.Errorf("strings with same content have different hash keys") | ||
} | ||
if diff1.MapKey() != diff2.MapKey() { | ||
t.Errorf("strings with same content have different hash keys") | ||
} | ||
if hello1.MapKey() == diff1.MapKey() { | ||
t.Errorf("strings with different content have same hash keys") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.