Skip to content

Commit

Permalink
support arguments on class methods call
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Aug 20, 2024
1 parent f3575e5 commit 7380c8d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
8 changes: 0 additions & 8 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,6 @@ func (interp *interpreter) newInstance(className string, args []Value) *ClassObj
}
classObject := class.(*ClassObject)

// Create a new instance of the class
//instance := &ClassObject{
// Name: classObject.Name,
// Parent: nil,
// Methods: classObject.Methods,
// Fields: make(map[string]Value),
//}

// Initialize fields or invoke constructor if necessary
// Check if the class has a constructor method
//if constructor, ok := classObject.Methods["constructor"]; ok {
Expand Down
8 changes: 4 additions & 4 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ func (e *PropertyAccess) Position() Position { return e.pos }

// MethodCall represents a method call on an object, e.g., `$object->method(arg1, arg2)`
type MethodCall struct {
pos Position // Position of the `->` operator in the source code
Object Expression // The object on which the method is being called
Method string // The name of the method being called
Arguments []string // Arguments passed to the method
pos Position // Position of the `->` operator in the source code
Object Expression // The object on which the method is being called
Method string // The name of the method being called
Arguments []Expression // Arguments passed to the method
}

func (e *MethodCall) expressionNode() {}
Expand Down
40 changes: 32 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,39 @@ func (p *parser) statement() Statement {
pos = p.pos
p.expect(OBJECT_OPERATOR)

methodName := p.val
p.expect(NAME)
args, _ := p.params()

expr = &MethodCall{pos, expr, p.val, args}
if p.tok == LPAREN {
pos := p.pos
p.next()
args := []Expression{}
gotComma := true
gotEllipsis := false
for p.tok != RPAREN && p.tok != EOF && !gotEllipsis {
if !gotComma {
p.error("expected , between arguments")
}
arg := p.expression()
args = append(args, arg)
if p.tok == ELLIPSIS {
gotEllipsis = true
p.next()
}
if p.tok == COMMA {
gotComma = true
p.next()
} else {
gotComma = false
}
}
if p.tok != RPAREN && gotEllipsis {
p.error("can only have ... after last argument")
}
p.expect(RPAREN)

expr = &MethodCall{pos, expr, methodName, args}
}

return &ExpressionStatement{pos, expr}

Expand Down Expand Up @@ -235,8 +264,8 @@ func (p *parser) params() ([]string, bool) {
if !gotComma {
p.error("expected , between parameters")
}
param := p.val
p.expect(DOLLAR)
param := p.val
p.expect(NAME)
params = append(params, param)
if p.tok == ELLIPSIS {
Expand Down Expand Up @@ -358,11 +387,6 @@ func (p *parser) call() Expression {
p.error("can only have ... after last argument")
}
p.expect(RPAREN)
//if p.tok == SEMI {
// p.expect(SEMI)
//} else {
// print(p.tok)
//}

expr = &Call{pos, expr, args, gotEllipsis}
} else if p.tok == LBRACKET {
Expand Down

0 comments on commit 7380c8d

Please sign in to comment.