Skip to content

Commit

Permalink
apply Declare.evalProperties to functions with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Feb 2, 2014
1 parent 68b7725 commit 42585ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/yinwang/yin/ast/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ public Call(Node func, Argument args, String file, int start, int end, int line,
public Value interp(Scope s) {
Value fun = this.func.interp(s);
if (fun instanceof Closure) {
Fun def = ((Closure) fun).fun;
Closure closure = (Closure) fun;
Scope funScope = new Scope(closure.env);
List<Name> params = closure.fun.params;

if (def.properties != null) {
Declare.evalProperties(def.properties, funScope);
}

if (!args.positional.isEmpty() && args.keywords.isEmpty()) {
// positional
if (args.positional.size() != params.size()) {
_.abort(this.func,
"calling function with wrong number of arguments: " + args.positional.size());
"calling function with wrong number of arguments. expected: " + params.size()
+ " actual: " + args.positional.size());
}

for (int i = 0; i < args.positional.size(); i++) {
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/yinwang/yin/ast/Declare.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import org.yinwang.yin.Constants;
import org.yinwang.yin.Scope;
import org.yinwang.yin._;
import org.yinwang.yin.parser.Parser;
import org.yinwang.yin.value.Value;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


Expand All @@ -23,6 +20,16 @@ public Declare(Scope propsNode, String file, int start, int end, int line, int c


public Value interp(Scope s) {
evalProperties(propsNode, s);
return Value.VOID;
}


// helper
// evaluate the properties inside propsNode
// then merge into the Scope s
public static void evalProperties(Scope propsNode, Scope s) {
// evaluate the properties
Scope properties = new Scope();
for (String field : propsNode.keySet()) {
Map<String, Object> props = propsNode.lookupAllProps(field);
Expand All @@ -32,13 +39,15 @@ public Value interp(Scope s) {
Value vValue = ((Node) v).interp(s);
properties.put(field, e.getKey(), vValue);
} else {
_.abort(this, "property is not a node, parser bug: " + v);
_.abort("property is not a node, parser bug: " + v);
}
}
}

// merge the properties into current scope
s.putAll(properties);

// set default values for variables
for (String key : properties.keySet()) {
Object defaultValue = properties.lookupPropertyLocal(key, "default");
if (defaultValue == null) {
Expand All @@ -49,11 +58,9 @@ public Value interp(Scope s) {
s.putValue(key, (Value) defaultValue);
}
} else {
_.abort("default value is not value, shouldn't happen");
_.abort("default value is not a value, shouldn't happen");
}
}

return Value.VOID;
}


Expand Down

0 comments on commit 42585ab

Please sign in to comment.