Coder Social home page Coder Social logo

Comments (15)

haifenghuang avatar haifenghuang commented on July 29, 2024

At the moment, magpie does not support the hash vivification, once I have time, I'll add support for it.

from magpie.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

no issue if i try to add that scenario then could you please where should I do changes.

from magpie.

haifenghuang avatar haifenghuang commented on July 29, 2024

There are a few places you need to change:

  1. In parser.go file, in the parseAssignExpression() method, when the name is of the type IndexExpression, you need to comment out the switch sentences.
  2. In eval.go file, in the evalHashKeyIndex(), you need to make some changes.

from magpie.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

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.

haifenghuang avatar haifenghuang commented on July 29, 2024

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.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

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.

haifenghuang avatar haifenghuang commented on July 29, 2024

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.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

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.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

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.

haifenghuang avatar haifenghuang commented on July 29, 2024

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.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

I am facing many problems. if is it possible to you, could you please add that functionality today

from magpie.

haifenghuang avatar haifenghuang commented on July 29, 2024

I'll figure it out how to do that, so sorry for that, maybe about two or three days will be fine.

from magpie.

Ashish1611 avatar Ashish1611 commented on July 29, 2024

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.

haifenghuang avatar haifenghuang commented on July 29, 2024

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.

haifenghuang avatar haifenghuang commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.