Commits

Anonymous committed d05da4d

make SExpression definition a lot shorter

Comments (0)

Files changed (3)

-/classes
-.ceylon/
-.settings/
-modules/
+classes
+.ceylon
+.settings
+modules
 *~

source/laamella/sexpressions/module.ceylon

 module laamella.sexpressions "1.0.0" {
 	shared import java.base "7";
 	import ceylon.io "0.6.1";
+	shared import ceylon.collection "0.6.1";
 }

source/laamella/sexpressions/sexprs.ceylon

 import java.lang { StringBuffer, IllegalStateException }
+import ceylon.collection { LinkedList }
 import java.io { Reader, StringReader }
 
-
-shared interface SExpression {
-	shared default String val {
-		throw;
-	}
-	
-	shared default SExpression? car(){
-		throw;
-	}
-	
-	shared default SExpression? get(Integer index){
-		throw; 
-	}
+shared class SExpression({SExpression|String*} values = {}) extends LinkedList<SExpression|String>(values){
 }
 
-shared class Atom(shared actual String string) satisfies SExpression {
-}
-
-shared class List() satisfies SExpression {
-	variable SExpression[] list=[];
-	
-	shared void add(SExpression sExpression) => list=list.withTrailing(sExpression);
-	shared actual SExpression? get(Integer index) => list[index];
-	
-	shared actual SExpression? car() {
-		return list[0];
-	}
-	
-	shared actual String string => list.string;
-}
-
-abstract class State() of atomState| whitespace| quotedAtom	{}
-object atomState extends State(){}
-object whitespace extends State(){}
-object quotedAtom extends State(){}
+abstract class ParseState() of inAtom | inWhitespace | inQuotedAtom {}
+object inAtom extends ParseState(){}
+object inWhitespace extends ParseState(){}
+object inQuotedAtom extends ParseState(){}
 
 shared class SExpressionParser() {	
 	shared SExpression parseExpressions(Reader reader) {
-		value list = List();
+		value list = SExpression();
 		variable Integer i;
 		while ((i = reader.read()) != -1) {
 			value c = i.character;
 	}
 
 	SExpression parseList(Reader reader) {
-		value root = List();
+		value root = SExpression();
 		variable Integer i;
-		variable State state = whitespace;
+		variable ParseState state = inWhitespace;
 		value atom = StringBuffer();
 		while ((i = reader.read()) != -1) {
 			value c =  i.character;
 			switch (state) 
-			case (whitespace) {
+			case (inWhitespace) {
 				if (!Character.whitespace(c)) {
 					if (c == '(') {
 						root.add(parseList(reader));
 						return root;
 					} else if (c == '"') {
 						atom.setLength(0);
-						state = quotedAtom;
+						state = inQuotedAtom;
 					} else {
 						atom.setLength(0);
 						atom.append(c);
-						state = atomState;
+						state = inAtom;
 					}
 				}
 			}
-			case (atomState) {
+			case (inAtom) {
 				if (Character.whitespace(c)) {
-					root.add(Atom(atom.string));
-					state = whitespace;
+					root.add(atom.string);
+					state = inWhitespace;
 				} else if (c == ')') {
-					root.add(Atom(atom.string));
+					root.add(atom.string);
 					return root;
 				} else {
 					atom.append(c);
 				}
 			}
-			case (quotedAtom) {
+			case (inQuotedAtom) {
 				if (c == '"') {
-					root.add(Atom(atom.string));
-					state = whitespace;
+					root.add(atom.string);
+					state = inWhitespace;
 				} else {
 					atom.append(c);
 				}
 }
 
 void test(){
-	value expr = SExpressionParser().parseExpression(StringReader("(a)"));
-	print(expr);
-	print(expr?.car()?.string);
+	SExpression? expr = SExpressionParser().parseExpression(StringReader("(a b c (d e f))"));
+	if(exists expr){
+		print(expr);
+		print(expr[0]?.string);
+		if(exists thirdAtom=expr[2]){
+			print(thirdAtom);
+		}
+	}
 }