Commits

firefly  committed b7ed95c

Unbreaks the parts.html file. tester.js is still broken; investigate!
FireLang execution is broken for unknown reason. Also, got to strip the outdated code out of Floop (old tokenizer).

  • Participants
  • Parent commits 13018b4

Comments (0)

Files changed (9)

 for file in `ls tests/*.floop`; do
   name=`echo $file | sed -r 's:tests/([^.]*).*:\1:'`
   
-  js -f tester-pre.js -f src/floop.js -f tester.js <$file >/dev/null 2>&1
+  js -f tester-pre.js -f src/floop.js -f tester.js <$file #>/dev/null 2>&1
   if [ $? -eq 0 ]; then
     echo "$? :: Test '$name' was successful."
   else

File src/floop.js

 
 	/* ---------- Floop ---------- */
 var Floop = {};
+	
+/*
+	// isValue and getValue for FireLang.
+function isValue(str) {
+	return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+			str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+}
+
+function getValue(obj) {
+	if (typeof(obj) != 'string') {
+		return obj;
+	} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+		return {value: obj.substr(1, obj.length-2)};
+	} else if (obj.match(/\d+/)) {
+		return {value: parseInt(obj)};
+	} else {
+		return obj;
+	}
+}
+
+	// FireLang Tokenizer descriptors
+function beginsIdentifier(c) {
+	return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+			c == "$" || c == "_";
+}
+function isNumber(c) {
+	return c >= "0" && c <= "9";
+}
+function isWhitespace(c) {
+	return c == " " || c == "\t" || c == "\n" || c == "\r";
+}
+function isIdentifier(c) {
+	return beginsIdentifier(c) || isNumber(c);
+}
+*/
 
 var sExpr, anyExpr;
 (function() {
 			return "<sExpr failed>";
 		}
 	};
-	
-	/*
-	function getValue(obj) {
-		if (typeof(obj) != 'string') {
-			return obj;
-		} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
-			return {value: obj.substr(1, obj.length-2)};
-		} else if (obj.match(/\d+/)) {
-			return {value: parseInt(obj)};
-		} else {
-			return obj;
-		}
-	}
-	*/
 
 	function getTuple(obj) {
 		for (var k in obj) return [k, obj[k]];
 				
 				if (desc.start(c)) {
 					var start = i;
-					for (i++; desc.cont(str.charAt(i), i-start) && i<str.length; i++);
+					for (i++; desc.cont(str.charAt(i), i-start, str.substr(start, i))
+							&& i<str.length; i++);
 					var end = i;
 					
 					if (!desc.ignore) {
 		throw new Error("Syntax error: " + reason + ". (near '" + error + "')");
 	}
 	
-	/*
-	function isValue(str) {
-		return typeof(str) == 'string' && (str.match(/^".*"$/) ||
-				str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
-	}
-	*/
-	
 	Grouper.prototype.add = function(op, func, prec) {
 		if (op instanceof Array) var op =
 				new Grouper.Operator(op, func, prec, this);

File src/floop.js~

 
 	/* ---------- Floop ---------- */
 var Floop = {};
+	
+/*
+	// isValue and getValue for FireLang.
+function isValue(str) {
+	return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+			str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+}
+
+function getValue(obj) {
+	if (typeof(obj) != 'string') {
+		return obj;
+	} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+		return {value: obj.substr(1, obj.length-2)};
+	} else if (obj.match(/\d+/)) {
+		return {value: parseInt(obj)};
+	} else {
+		return obj;
+	}
+}
+
+	// FireLang Tokenizer descriptors
+function beginsIdentifier(c) {
+	return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+			c == "$" || c == "_";
+}
+function isNumber(c) {
+	return c >= "0" && c <= "9";
+}
+function isWhitespace(c) {
+	return c == " " || c == "\t" || c == "\n" || c == "\r";
+}
+function isIdentifier(c) {
+	return beginsIdentifier(c) || isNumber(c);
+}
+*/
 
 var sExpr, anyExpr;
 (function() {
 			return "<sExpr failed>";
 		}
 	};
-	
-	/*
-	function getValue(obj) {
-		if (typeof(obj) != 'string') {
-			return obj;
-		} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
-			return {value: obj.substr(1, obj.length-2)};
-		} else if (obj.match(/\d+/)) {
-			return {value: parseInt(obj)};
-		} else {
-			return obj;
-		}
-	}
-	*/
 
 	function getTuple(obj) {
 		for (var k in obj) return [k, obj[k]];
 				
 				if (desc.start(c)) {
 					var start = i;
-					for (i++; desc.cont(str.charAt(i), i-start) && i<str.length; i++);
+					for (i++; desc.cont(str.charAt(i), i-start, str.substr(start, i))
+							&& i<str.length; i++);
 					var end = i;
 					
 					if (!desc.ignore) {
 						tokens.push(str.substring(start, end));
 					}
-				//	i--;
+					i--;
 					
 					continue outer;
 				}
 		throw new Error("Syntax error: " + reason + ". (near '" + error + "')");
 	}
 	
-	/*
-	function isValue(str) {
-		return typeof(str) == 'string' && (str.match(/^".*"$/) ||
-				str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
-	}
-	*/
-	
 	Grouper.prototype.add = function(op, func, prec) {
 		if (op instanceof Array) var op =
 				new Grouper.Operator(op, func, prec, this);

File src/interpreter.html

 	};
 	
 	(function() {
+			/* Tokenizer and Grouper functions. */
+		function beginsIdentifier(c) {
+			return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+					c == "$" || c == "_";
+		}
+		function isNumber(c) {
+			return c >= "0" && c <= "9";
+		}
+		function isWhitespace(c) {
+			return c == " " || c == "\t" || c == "\n" || c == "\r";
+		}
+		function isIdentifier(c) {
+			return beginsIdentifier(c) || isNumber(c);
+		}
+		
+		function isValue(str) {
+			return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+					str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+		}
+
+		function getValue(obj) {
+			if (typeof(obj) != 'string') {
+				return obj;
+			} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+				return {value: obj.substr(1, obj.length-2)};
+			} else if (obj.match(/\d+/)) {
+				return {value: parseInt(obj)};
+			} else {
+				return obj;
+			}
+		}
+		
+			/* Helper functions */
 		function isArray(obj) {
 			return typeof(obj) == 'object' && obj !== null && obj instanceof Array;
 		}
 			}, args);
 		}
 		
+		var tokenizer = new Floop.Tokenizer();
+		tokenizer.add("identifier", beginsIdentifier, isIdentifier);
+		tokenizer.add("number", isNumber, isNumber);
+		tokenizer.add("string", function(c) {
+			return c == '"';
+		}, function(c, i, str) {
+			return i == 1 || (str.charAt(i-1) != '"' && str.charAt(i-2) != "\\");
+		});
+		tokenizer.add("whitespace", isWhitespace, isWhitespace, true);
+		tokenizer.add("operator", function() {return true;}, function() {return false;});
+		
 		FireLang.prototype.interpret = function(str) {
-			var stm = grouper.group(Floop.tokenize(str));
-			//	var stms = str.split(';');
-			
-		//	for (var i=0; i<stms.length; i++) {
-		//		var stm = grouper.group(Floop.tokenize(stms[i]));
-		//		execute(this, stm);
-		//	}
+			var stm = grouper.group(tokenizer.tokenize(str));
 		};
 		
-		var grouper = new Floop.Grouper();
+		var grouper = new Floop.Grouper(isValue, getValue);
 		
 		function fun(name, func) {
 			func.name = name;

File src/interpreter.html~

 	};
 	
 	(function() {
+			/* Tokenizer and Grouper functions. */
+		function beginsIdentifier(c) {
+			return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+					c == "$" || c == "_";
+		}
+		function isNumber(c) {
+			return c >= "0" && c <= "9";
+		}
+		function isWhitespace(c) {
+			return c == " " || c == "\t" || c == "\n" || c == "\r";
+		}
+		function isIdentifier(c) {
+			return beginsIdentifier(c) || isNumber(c);
+		}
+		
+		function isValue(str) {
+			return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+					str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+		}
+
+		function getValue(obj) {
+			if (typeof(obj) != 'string') {
+				return obj;
+			} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+				return {value: obj.substr(1, obj.length-2)};
+			} else if (obj.match(/\d+/)) {
+				return {value: parseInt(obj)};
+			} else {
+				return obj;
+			}
+		}
+		
+			/* Helper functions */
 		function isArray(obj) {
 			return typeof(obj) == 'object' && obj !== null && obj instanceof Array;
 		}
 			}, args);
 		}
 		
+		var tokenizer = new Floop.Tokenizer();
+		tokenizer.add("identifier", beginsIdentifier, isIdentifier);
+		tokenizer.add("number", isNumber, isNumber);
+		tokenizer.add("string", function(c) {
+			return c == '"';
+		}, function(c, i, str) {
+			return i == 1 || str.charAt(i-1) != '"';
+		});
+		tokenizer.add("whitespace", isWhitespace, isWhitespace, true);
+		tokenizer.add("operator", function() {return true;}, function() {return false;});
+		
 		FireLang.prototype.interpret = function(str) {
-			var stm = grouper.group(Floop.tokenize(str));
-			//	var stms = str.split(';');
-			
-		//	for (var i=0; i<stms.length; i++) {
-		//		var stm = grouper.group(Floop.tokenize(stms[i]));
-		//		execute(this, stm);
-		//	}
+			var stm = grouper.group(tokenizer.tokenize(str));
 		};
 		
-		var grouper = new Floop.Grouper();
+		var grouper = new Floop.Grouper(isValue, getValue);
 		
 		function fun(name, func) {
 			func.name = name;

File src/parts.html

 	
 	var Data = {};
 	
+	function isValue(str) {
+		return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+				str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+	}
+	
+	function getValue(obj) {
+		if (typeof(obj) != 'string') {
+			return obj;
+		} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+			return {value: obj.substr(1, obj.length-2)};
+		} else if (obj.match(/\d+/)) {
+			return {value: parseInt(obj)};
+		} else {
+			return obj;
+		}
+	}
+	
+	function beginsIdentifier(c) {
+		return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+				c == "$" || c == "_";
+	}
+	function isNumber(c) {
+		return c >= "0" && c <= "9";
+	}
+	function isWhitespace(c) {
+		return c == " " || c == "\t" || c == "\n" || c == "\r";
+	}
+	function isIdentifier(c) {
+		return beginsIdentifier(c) || isNumber(c);
+	}
+	
 	function updateOperators(el) {
 		var ops = el.value.split(/\n/g);
 		
-		Data.grouper = new Floop.Grouper();
+		Data.grouper = new Floop.Grouper(isValue, getValue);
 		var none = function() {};
 		
 		function isIdentifier(str) {
 			return str.match(/^[A-Za-z\d_$]\w*$/);
 		}
 		
+		var tkz = new Floop.Tokenizer();
+		tkz.add("identifier", beginsIdentifier, isIdentifier);
+		tkz.add("number", isNumber, isNumber);
+		tkz.add("string", function(c) {
+			return c == '"';
+		}, function(c, i, str) {
+			return i == 1 || (str.charAt(i-1) != '"' && str.charAt(i-2) != "\\");
+		});
+		tkz.add("whitespace", isWhitespace, isWhitespace, true);
+		tkz.add("operator", function() {return true;}, function() {return false;});
+		
 		for (var i=0; i<ops.length; i++) {
 			var tmp = ops[i].match(/^\s*([\d.]+)\s*{\s*([^}]*)\s*}\s*"([^"]*)"\s*$/);
 			if (!tmp) continue;
 			
-			var toks = Floop.tokenize(tmp[2]);
+			var toks = tkz.tokenize(tmp[2]);
 			
 			toks = toks.map(function(a) {
 				if (isIdentifier(a)) {

File src/parts.html~

 	
 	var Data = {};
 	
+	function isValue(str) {
+		return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+				str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+	}
+
+	function getValue(obj) {
+		if (typeof(obj) != 'string') {
+			return obj;
+		} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+			return {value: obj.substr(1, obj.length-2)};
+		} else if (obj.match(/\d+/)) {
+			return {value: parseInt(obj)};
+		} else {
+			return obj;
+		}
+	}
+	
+	function beginsIdentifier(c) {
+		return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+				c == "$" || c == "_";
+	}
+	function isNumber(c) {
+		return c >= "0" && c <= "9";
+	}
+	function isWhitespace(c) {
+		return c == " " || c == "\t" || c == "\n" || c == "\r";
+	}
+	function isIdentifier(c) {
+		return beginsIdentifier(c) || isNumber(c);
+	}
+	
 	function updateOperators(el) {
 		var ops = el.value.split(/\n/g);
 		
-		Data.grouper = new Floop.Grouper();
+		Data.grouper = new Floop.Grouper(isValue, getValue);
 		var none = function() {};
 		
 		function isIdentifier(str) {
 			return str.match(/^[A-Za-z\d_$]\w*$/);
 		}
 		
+		var tkz = new Floop.Tokenizer();
+		tkz.add("identifier", beginsIdentifier, isIdentifier);
+		tkz.add("number", isNumber, isNumber);
+		tkz.add("string", function(c) {
+			return c == '"';
+		}, function(c, i, str) {
+			return i == 1 || (str.charAt(i-1) != '"' && str.charAt(i-2) != "\\");
+		});
+		tkz.add("whitespace", isWhitespace, isWhitespace, true);
+		tkz.add("operator", function() {return true;}, function() {return false;});
+		
 		for (var i=0; i<ops.length; i++) {
 			var tmp = ops[i].match(/^\s*([\d.]+)\s*{\s*([^}]*)\s*}\s*"([^"]*)"\s*$/);
 			if (!tmp) continue;
 			
-			var toks = Floop.tokenize(tmp[2]);
+			var toks = tkz.tokenize(tmp[2]);
 			
 			toks = toks.map(function(a) {
 				if (isIdentifier(a)) {
 5.4 {a + b} "+"
 
 7 {a "in" b} "in"
+8 {a ? b : c} "iif"
 
 9 {a , b} "tuple"
 10 {$x = a} "assign"</textarea>
 	return str.match(/^[A-Za-z\d_$]\w*$/);
 }
 
+function isValue(str) {
+	return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+			str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+}
+
+function getValue(obj) {
+	if (typeof(obj) != 'string') {
+		return obj;
+	} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+		return {value: obj.substr(1, obj.length-2)};
+	} else if (obj.match(/\d+/)) {
+		return {value: parseInt(obj)};
+	} else {
+		return obj;
+	}
+}
+
+function beginsIdentifier(c) {
+	return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+			c == "$" || c == "_";
+}
+function isNumber(c) {
+	return c >= "0" && c <= "9";
+}
+function isWhitespace(c) {
+	return c == " " || c == "\t" || c == "\n" || c == "\r";
+}
+function isIdentifier(c) {
+	return beginsIdentifier(c) || isNumber(c);
+}
+
 function testFloop(syntax, input, expected) {
 		/* Prepare grouper */
 	var ops = syntax.split(/\n/g);
 
-	var grouper = new Floop.Grouper();
+	var tkz = new Floop.Tokenizer();
+	var grouper = new Floop.Grouper(isValue, getValue);
+	
+	var tkz = new Floop.Tokenizer();
+	tkz.add("identifier", beginsIdentifier, isIdentifier);
+	tkz.add("number", isNumber, isNumber);
+	tkz.add("string", function(c) {
+		return c == '"';
+	}, function(c, i, str) {
+		return i == 1 || (str.charAt(i-1) != '"' && str.charAt(i-2) != "\\");
+	});
+	tkz.add("whitespace", isWhitespace, isWhitespace, true);
+	tkz.add("operator", function() {return true;}, function() {return false;});
+	
 	var none = function() {};
 	
 	for (var i=0; i<ops.length; i++) {
 		var tmp = ops[i].match(/^\s*([\d.]+)\s*{\s*([^}]*)\s*}\s*"([^"]*)"\s*$/);
 		if (!tmp) continue;
 		
-		var toks = Floop.tokenize(tmp[2]);
+		var toks = tkz.tokenize(tmp[2]);
 		
 		toks = toks.map(function(a) {
 			if (isIdentifier(a)) {
 	}).join("\n");
 	
 	if (output + "\n" != expected) {
+		print(output);
+		print(expected);
 		throw new Error("Mismatch!");
 	} else {
 		print("Success!");
 	return str.match(/^[A-Za-z\d_$]\w*$/);
 }
 
+function isValue(str) {
+	return typeof(str) == 'string' && (str.match(/^".*"$/) ||
+			str.match(/^[A-Za-z\d_$]\w*$/) || str.match(/^\d+/));
+}
+
+function getValue(obj) {
+	if (typeof(obj) != 'string') {
+		return obj;
+	} else if (obj.match(/^"(?:[^"]|(?=<\\)")*"$/)) {
+		return {value: obj.substr(1, obj.length-2)};
+	} else if (obj.match(/\d+/)) {
+		return {value: parseInt(obj)};
+	} else {
+		return obj;
+	}
+}
+
+function beginsIdentifier(c) {
+	return c >= "a" && c <= "z" || c >= "A" && c <= "Z" ||
+			c == "$" || c == "_";
+}
+function isNumber(c) {
+	return c >= "0" && c <= "9";
+}
+function isWhitespace(c) {
+	return c == " " || c == "\t" || c == "\n" || c == "\r";
+}
+function isIdentifier(c) {
+	return beginsIdentifier(c) || isNumber(c);
+}
+
 function testFloop(syntax, input, expected) {
 		/* Prepare grouper */
 	var ops = syntax.split(/\n/g);
 
-	var grouper = new Floop.Grouper();
+	var tkz = new Floop.Tokenizer();
+	var grouper = new Floop.Grouper(isValue, getValue);
+	
+	var tkz = new Floop.Tokenizer();
+	tkz.add("identifier", beginsIdentifier, isIdentifier);
+	tkz.add("number", isNumber, isNumber);
+	tkz.add("string", function(c) {
+		return c == '"';
+	}, function(c, i, str) {
+		return i == 1 || (str.charAt(i-1) != '"' && str.charAt(i-2) != "\\");
+	});
+	tkz.add("whitespace", isWhitespace, isWhitespace, true);
+	tkz.add("operator", function() {return true;}, function() {return false;});
+	
 	var none = function() {};
 	
 	for (var i=0; i<ops.length; i++) {
 		var tmp = ops[i].match(/^\s*([\d.]+)\s*{\s*([^}]*)\s*}\s*"([^"]*)"\s*$/);
 		if (!tmp) continue;
 		
-		var toks = Floop.tokenize(tmp[2]);
+		var toks = tkz.tokenize(tmp[2]);
 		
 		toks = toks.map(function(a) {
 			if (isIdentifier(a)) {
 		
 		if (buffer.match(/^\s*$/)) {
 			continue;
-		} else if (buffer.match(/^\s*-+\s*$/)) {
+		} else if (buffer.match(/^\s*--+\s*$/)) {
 			i++;
 			continue;
 		} else {