Comments (15)
At the moment, magpie does not support the hash vivification, once I have time, I'll add support for it.
from magpie.
no issue if i try to add that scenario then could you please where should I do changes.
from magpie.
There are a few places you need to change:
- In
parser.go
file, in theparseAssignExpression()
method, when thename
is of the typeIndexExpression
, you need to comment out theswitch
sentences. - In
eval.go
file, in theevalHashKeyIndex()
, you need to make some changes.
from magpie.
how we will check that name type is that is a index-expression or not,
actually i am new so for practice purpose
from magpie.
You could use code like below to check the type:
if indexExp, ok := name.(*ast.IndexExpression); ok { // It's a index type
}
Here, we use golang
's type assertion to check the type.
from magpie.
i have did something like this
`a := &ast.AssignExpression{Token: p.curToken}
switch name.(type) {
case *ast.Identifier:
a.Name = name
case *ast.IndexExpression:
switch name.(*ast.IndexExpression).Left.(type) {
case *ast.Identifier, *ast.IndexExpression:
a.Name = name.(*ast.IndexExpression)
default:
msg := fmt.Sprintf("index assigment expects an identifier, got [%T] at lineNumber[%d], charPosition[%d]", name.(*ast.IndexExpression).Left, p.curToken.LineNumber, p.curToken.LinePos)
p.errors = append(p.errors, msg)
return a
}
default:
msg := fmt.Sprintf("expected assigment token to be an identifier, got [%T] at lineNumber[%d], charPosition[%d]", name, p.curToken.LineNumber, p.curToken.LinePos)
p.errors = append(p.errors, msg)
return a
}
p.nextToken()
a.Value = p.parseExpression(LOWEST)
return a`
from magpie.
I think it's right. BTW, you can debug the evaluation stage by uncommenting the line 84
of eval.go
file, so you can see the evaluated ast
code. And if you need any help, please let me know, Maybe I could help.
from magpie.
this is my valHashIndex func
`func evalHashIndexExpression(ctx context.Context, hash, index object.Object) object.Object {
select {
case <-ctx.Done():
return &object.Exit{}
default:
hashObject := hash.(*object.Hash)
key, ok := index.(object.Hashable)
if !ok {
return error.NewError("cannot use '%s' as hash key", index.Type())
}
pair, ok := hashObject.Pairs[key.HashKey()]
if !ok {
return object.NULL
}
return pair.Value
}
}`
and following is my eval hash literal func
`func evalHashLiteral(ctx context.Context, node *ast.HashLiteral, env *object.Environment) object.Object {
select {
case <-ctx.Done():
return &object.Exit{}
default:
pairs := make(map[object.HashKey]object.HashPair)
for keyNode, valueNode := range node.Pairs {
key := Eval(ctx, keyNode, env)
if error.IsError(key) {
return key
}
hashKey, ok := key.(object.Hashable)
if !ok {
return error.NewError("cannot use '%s' as hash key", key.Type())
}
value := Eval(ctx, valueNode, env)
if error.IsError(value) {
return value
}
hashed := hashKey.HashKey()
pairs[hashed] = object.HashPair{Key: key, Value: value}
}
return &object.Hash{Pairs: pairs}
}
}`
from magpie.
and following is eval index func
func evalIndexExpression(ctx context.Context, left, index object.Object) object.Object { select { case <-ctx.Done(): return &object.Exit{} default: switch { case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: return evalArrayIndexExpression(ctx, left, index) case left.Type() == object.HASH_OBJ: fmt.Println("left from index expression", left) return evalHashIndexExpression(ctx, left, index) default: return error.NewError("index operator not supported: %s", left.Type()) } } }
from magpie.
Thanks a lot for the code, but I'm not very clear about the code, for example context.Context
and object.Exit
.
You could make a pull request and make sure test it.
from magpie.
I am facing many problems. if is it possible to you, could you please add that functionality today
from magpie.
I'll figure it out how to do that, so sorry for that, maybe about two or three days will be fine.
from magpie.
In evalhashkeyindex() i am doing but somehow the changes wont get worked could you please suggest me some changes so that i can try to implement it.
from magpie.
In the evalhashkeyindex
, you can check the ie.Left.(type)
, in your example, i.e. hash["one"]["two"], the ie.Left.(type)
is *ast.IndexExpression
, maybe that will give you some hints.
BTW, you can print the variable's type using %T
:
fmt.Printf("xxxx's type is %T\n", xxxx)
Here are some hints for you:
//if variable is a Node, you can print it's variable using Node.String() method
fmt.Printf("node.vaule = %s\n", node.String())
//if variable is an Object, you can print it's variable using Object.Inspect() method
fmt.Printf("object.value=%s\n", object.Inspect())
from magpie.
Here are some code:
func evalHashKeyIndex(hash *Hash, ie *ast.IndexExpression, scope *Scope) Object {
key := Eval(ie.Index, scope)
if key.Type() == ERROR_OBJ {
return key
}
result := hash.Get(ie.Pos().Sline(), key)
if result == NIL {
//hash["one"]["two"]
// hash = {
// "one": {
// "two": {}
// }
// }
switch l := ie.Left.(type) {
case *ast.Identifier:
result := NewHash()
hash.Push(ie.Pos().Sline(), key, result)
return result
case *ast.IndexExpression:
var ok bool
//var result Object
_, ok = l.Left.(*ast.IndexExpression);
if ok {
//How to do this?
} else {
idxExpr := &ast.IndexExpression{Left: l.Left, Index: ie.Index}
return evalHashKeyIndex(Eval(l, scope).(*Hash), idxExpr, scope)
}
return hash
default:
return NIL
}
} else {
return result
}
}
The above code could solve the problem like hash["one"]["two"]
which has two levels,
Right now, I cannot figure it out when the hash is three or more levels,
from magpie.
Related Issues (17)
- Question: have you planned to make Magpie compilable or bytecode? HOT 2
- Question: When do you want to implement interface, public, private, protected keywords? HOT 1
- [REPL Bug] stdin does not scans full string. HOT 1
- [REPL Bug] include errors ignoring. HOT 1
- How to check the typeof obj ? HOT 2
- Could Break to Label? HOT 1
- Is there go-routine feature? HOT 1
- howto get properties from json object HOT 2
- Hello HOT 1
- tcp的read方法阻塞 HOT 1
- idea: add namespace management HOT 6
- Please add multiline code blocks to the REPL. HOT 5
- Errors in tests. HOT 4
- I found a way to add support for highlight on Windows HOT 2
- Question: let keyword HOT 3
- Compilation succeed for Windows but failed for other platforms. HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magpie.