Skip to content

Commit

Permalink
factor out a bit more things
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Feb 3, 2014
1 parent 819b8a8 commit 34f28ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/yin/ast/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Value interp(Scope s) {
List<Name> params = closure.fun.params;

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

if (!args.positional.isEmpty() && args.keywords.isEmpty()) {
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/org/yinwang/yin/ast/Declare.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,24 @@ public Declare(Scope propsNode, String file, int start, int end, int line, int c


public Value interp(Scope s) {
evalProperties(propsNode, s);
mergeProperties(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) {
public static void mergeProperties(Scope unevaled, Scope s) {
// evaluate the properties
Scope properties = new Scope();
for (String field : propsNode.keySet()) {
Map<String, Object> props = propsNode.lookupAllProps(field);
for (Map.Entry<String, Object> e : props.entrySet()) {
Object v = e.getValue();
if (v instanceof Node) {
Value vValue = ((Node) v).interp(s);
properties.put(field, e.getKey(), vValue);
} else {
_.abort("property is not a node, parser bug: " + v);
}
}
}
Scope evaled = evalProperties(unevaled, s);

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

// set default values for variables
for (String key : properties.keySet()) {
Object defaultValue = properties.lookupPropertyLocal(key, "default");
for (String key : evaled.keySet()) {
Object defaultValue = evaled.lookupPropertyLocal(key, "default");
if (defaultValue == null) {
continue;
} else if (defaultValue instanceof Value) {
Expand All @@ -64,6 +52,25 @@ public static void evalProperties(Scope propsNode, Scope s) {
}


public static Scope evalProperties(Scope unevaled, Scope s) {
Scope evaled = new Scope();

for (String field : unevaled.keySet()) {
Map<String, Object> props = unevaled.lookupAllProps(field);
for (Map.Entry<String, Object> e : props.entrySet()) {
Object v = e.getValue();
if (v instanceof Node) {
Value vValue = ((Node) v).interp(s);
evaled.put(field, e.getKey(), vValue);
} else {
_.abort("property is not a node, parser bug: " + v);
}
}
}
return evaled;
}


public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Constants.TUPLE_BEGIN);
Expand Down

0 comments on commit 34f28ee

Please sign in to comment.