Skip to content

Commit

Permalink
define and assign into records
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Jan 21, 2014
1 parent 4243a84 commit 2acdbe9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/yinwang/yin/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


import org.yinwang.yin.ast.*;
import org.yinwang.yin.value.Record;
import org.yinwang.yin.value.Value;
import org.yinwang.yin.value.Vector;

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

public class Binder {

Expand All @@ -18,6 +20,19 @@ public static void define(Node pattern, Value value, Scope env) {
} else {
env.put(id, value);
}
} else if (pattern instanceof RecordLiteral) {
if (value instanceof Record) {
Map<String, Node> elms1 = ((RecordLiteral) pattern).map;
Map<String, Value> elms2 = ((Record) value).values;
if (elms1.keySet().equals(elms2.keySet())) {
for (String k1 : elms1.keySet()) {
define(elms1.get(k1), elms2.get(k1), env);
}
} else {
_.abort(pattern, "define with records of different attributes: " +
elms1.keySet() + " v.s. " + elms2.keySet());
}
}
} else if (pattern instanceof VectorLiteral) {
if (value instanceof Vector) {
List<Node> elms1 = ((VectorLiteral) pattern).elements;
Expand Down Expand Up @@ -51,6 +66,19 @@ public static void assign(Node pattern, Value value, Scope env) {
((Subscript) pattern).set(value, env);
} else if (pattern instanceof Attr) {
((Attr) pattern).set(value, env);
} else if (pattern instanceof RecordLiteral) {
if (value instanceof Record) {
Map<String, Node> elms1 = ((RecordLiteral) pattern).map;
Map<String, Value> elms2 = ((Record) value).values;
if (elms1.keySet().equals(elms2.keySet())) {
for (String k1 : elms1.keySet()) {
assign(elms1.get(k1), elms2.get(k1), env);
}
} else {
_.abort(pattern, "assign with records of different attributes: " +
elms1.keySet() + " v.s. " + elms2.keySet());
}
}
} else if (pattern instanceof VectorLiteral) {
if (value instanceof Vector) {
List<Node> elms1 = ((VectorLiteral) pattern).elements;
Expand Down
8 changes: 8 additions & 0 deletions tests/assign1.yin
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
(set! bb.x 10)
[aa bb]


(define [x y] [1 2])
[x y]

(set! [x y] [y x])
[x y]

(define {:a xx :b yy} {:a 7 :b 8})
[xx yy]

(set! {:a x :b y} {:a 7 :b 8})
[x y]

0 comments on commit 2acdbe9

Please sign in to comment.