Skip to content

Commit

Permalink
support a second kind of parameters
Browse files Browse the repository at this point in the history
- of the form [x :type Int :default 1]
  • Loading branch information
yinwang0 committed Feb 2, 2014
1 parent cddcabd commit 68b7725
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/yinwang/yin/ast/Fun.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
public class Fun extends Node {
public List<Name> params;
public Node body;
public Scope properties;


public Fun(List<Name> params, Node body, String file, int start, int end, int line, int col) {
public Fun(List<Name> params, Scope properties, Node body, String file, int start, int end, int line, int col) {
super(file, start, end, line, col);
this.params = params;
this.properties = properties;
this.body = body;
}

Expand Down
39 changes: 33 additions & 6 deletions src/main/java/org/yinwang/yin/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,40 @@ public static Node parseNode(Node prenode) {
_.abort(preParams, "incorrect format of parameters: " + preParams);
}

// parse the parameters, test whether it's all names or all tuples
boolean hasName = false;
boolean hasTuple = false;
List<Name> paramNames = new ArrayList<>();
List<Node> paramTuples = new ArrayList<>();

for (Node p : ((Tuple) preParams).elements) {
Node parsed = parseNode(p);
if (!(parsed instanceof Name)) {
_.abort(parsed, "parameter must be a name");
if (p instanceof Name) {
hasName = true;
paramNames.add((Name) p);
} else if (p instanceof Tuple) {
hasTuple = true;
List<Node> argElements = ((Tuple) p).elements;
if (argElements.size() == 0) {
_.abort(p, "illegal argument format: " + p);
}
if (!(argElements.get(0) instanceof Name)) {
_.abort(p, "illegal argument name : " + argElements.get(0));
}
paramNames.add((Name) argElements.get(0));
paramTuples.add(p);
}
paramNames.add((Name) parsed);
}

if (hasName && hasTuple) {
_.abort(preParams, "parameters must be either all names or all tuples: " + preParams);
return null;
}

Scope properties;
if (hasTuple) {
properties = parseProperties(paramTuples);
} else {
properties = null;
}

// construct body
Expand All @@ -129,8 +156,8 @@ public static Node parseNode(Node prenode) {
int end = statements.get(statements.size() - 1).end;
Node body = new Block(statements, prenode.file, start, end, prenode.line, prenode.col);

return new Fun(paramNames, body, prenode.file, prenode.start, prenode.end,
prenode.line, prenode.col);
return new Fun(paramNames, properties, body,
prenode.file, prenode.start, prenode.end, prenode.line, prenode.col);
}

// -------------------- record type definition --------------------
Expand Down

0 comments on commit 68b7725

Please sign in to comment.