Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Aug 19, 2024
1 parent cc8c04f commit d9d5bcd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
3 changes: 3 additions & 0 deletions interpreter/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ func typeName(v Value) string {
t = "map"
case functionType:
t = "function"
case *userObject:
t = "object"

default:
// Interpreter should never give us this
panic(fmt.Sprintf("type() got unexpected type %T", v))
Expand Down
35 changes: 32 additions & 3 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,34 @@ func (interp *interpreter) evaluate(expr parser.Expression) Value {
case *parser.SemiTag:
return nil
case *parser.MethodCall:
// Evaluate the object and method name
print("Method call: ", e.Method)
return nil

// The method name
methodName := e.Method

print("Method name: ", methodName)

// Evaluate the object on which the method is being called
object := interp.evaluate(e.Object)
//print("Object: ", object)

//// Evaluate arguments passed to the method
args := []Value{}
for _, arg := range e.Arguments {
args = append(args, interp.evaluate(arg))
}
//
//// Check if the object is of a type that supports method calls
if objWithMethods, ok := object.(objectWithMethodsType); ok {
// Call the method on the object
method := objWithMethods.lookupMethod(methodName)
if method == nil {
panic(typeError(e.Position(), "method %q not found on type %s", methodName, typeName(object)))
}

return interp.callFunction(e.Position(), method, args)
}
//
panic(typeError(e.Position(), "type %s does not support method calls", typeName(object)))

case *parser.NewExpression:
// Evaluate the class name and arguments
Expand All @@ -469,6 +494,10 @@ func (interp *interpreter) evaluate(expr parser.Expression) Value {
}
}

type objectWithMethodsType interface {
lookupMethod(methodName string) functionType
}

func (interp *interpreter) pushScope(scope map[string]Value) {
interp.vars = append(interp.vars, scope)
}
Expand Down
55 changes: 34 additions & 21 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func (p *parser) statement() Statement {
}
pos := p.pos
expr := p.expression()
if p.tok == OBJECT_OPERATOR {
pos = p.pos
p.expect(OBJECT_OPERATOR)

expr = &MethodCall{pos, expr, p.val, []Expression{}}

p.expect(NAME)
p.expect(LPAREN)
p.expect(RPAREN)

return &ExpressionStatement{pos, expr}

}
if p.tok == ASSIGN {
pos = p.pos
switch expr.(type) {
Expand Down Expand Up @@ -442,27 +455,27 @@ func (p *parser) primary() Expression {
args, _ := p.params()

return &NewExpression{pos, className, args}
case OBJECT_OPERATOR:
pos := p.pos
p.next() // Move past the OBJECT_OPERATOR token

if p.tok != NAME {
p.error("expected a method or property name after '->'")
return nil
}

methodName := p.val
p.next() // Move past the method name

//// Assuming you want to handle method calls like $test->call(args...)
if p.tok == LPAREN {
p.next() // Skip '('
args := []Expression{}
p.expect(RPAREN) // Ensure method call is closed with ')'
return &MethodCall{pos, nil, methodName, args}
}

return &PropertyAccess{pos, nil, methodName}
//case OBJECT_OPERATOR:
// pos := p.pos
// p.next() // Move past the OBJECT_OPERATOR token
//
// if p.tok != NAME {
// p.error("expected a method or property name after '->'")
// return nil
// }
//
// methodName := p.val
// p.next() // Move past the method name
//
// //// Assuming you want to handle method calls like $test->call(args...)
// if p.tok == LPAREN {
// p.next() // Skip '('
// args := []Expression{}
// p.expect(RPAREN) // Ensure method call is closed with ')'
// return &MethodCall{pos, nil, methodName, args}
// }
//
// return &PropertyAccess{pos, nil, methodName}

default:
formatter := prettyjson.NewFormatter()
Expand Down

0 comments on commit d9d5bcd

Please sign in to comment.