functions with arguments
This commit is contained in:
@@ -265,6 +265,10 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
case *ast.FunctionLiteral:
|
||||
c.enterScope()
|
||||
|
||||
for _, p := range node.Parameters {
|
||||
c.symbolTable.Define(p.Value)
|
||||
}
|
||||
|
||||
err := c.Compile(node.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -280,7 +284,11 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
numLocals := c.symbolTable.numDefinitions
|
||||
instructions := c.leaveScope()
|
||||
|
||||
compiledFn := &object.CompiledFunction{Instructions: instructions, NumLocals: numLocals}
|
||||
compiledFn := &object.CompiledFunction{
|
||||
Instructions: instructions,
|
||||
NumLocals: numLocals,
|
||||
NumParameters: len(node.Parameters),
|
||||
}
|
||||
c.emit(code.OpConstant, c.addConstant(compiledFn))
|
||||
|
||||
case *ast.ReturnStatement:
|
||||
@@ -297,7 +305,14 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.emit(code.OpCall)
|
||||
for _, a := range node.Arguments {
|
||||
err := c.Compile(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
c.emit(code.OpCall, len(node.Arguments))
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user