Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function call scope error #13

Open
haifenghuang opened this issue Jun 7, 2017 · 1 comment
Open

Function call scope error #13

haifenghuang opened this issue Jun 7, 2017 · 1 comment

Comments

@haifenghuang
Copy link

I encounted an error regarding function all, in evalFunctionCall function:

func evalFunctionCall(call *ast.CallExpression, s *Scope) Object {
	fn, ok := s.Get(call.Function.String())
	if !ok {
		if f, ok := call.Function.(*ast.FunctionLiteral); ok {
			fn = &Function{Literal: f, Scope: s}
			s.Set(call.Function.String(), fn)
		} else if builtin, ok := builtins[call.Function.String()]; ok {
			return builtin.Fn(evalArgs(call.Arguments, s)...)
		} else {
			return newError(UNKNOWNIDENT, call.Function.String())
		}
	}
	f := fn.(*Function)
	f.Scope = NewScope(s)
	args := evalArgs(call.Arguments, f.Scope)
	// TODO: If not enough of arguments are passed a panic occur, if too few, no warning or error
	for i, v := range f.Literal.Parameters {
		f.Scope.Set(v.String(), args[i])
	}
	r := Eval(f.Literal.Body, f.Scope)
	if obj, ok := r.(*ReturnValue); ok {
		return obj.Value
	}
	return r
}

There is a problem concerning the scope of the function all.

Below is the test program:

let pp = fn(x, y) { fn(z) { x + y + z } }
let zz = pp(1,2)
putln(zz(3))

With above code, it will issue an error

Err: unknown identifier: 'x' is not defined

I think the above line

f.Scope = NewScope(s)

is not correct. It should be:

f.Scope = NewScope(f.Scope)

With this change, it outputs 6 which is correct.

By the way, based on your code, i've added some new features:

  1. one-line comment(#)
  2. Limited floating point support
  3. While loop
  4. Continue support
  5. Regexp support( Which is ugly. regexp literal should enclosed in ~, not /)
  6. And some minor bug fixes

If time is allowed, i will a new PR.

@mayoms
Copy link
Owner

mayoms commented Jun 9, 2017

Ok, I'll take a look at this this weekend. PRs are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants