changeset 44:9dd370530f69

Fix escape codes in string literals. Don't print out the return value of main method. Fixup fib example to use print: method. Cleanup error handling in compiler slightly
author Mike Pavone <pavone@retrodev.com>
date Thu, 12 Jul 2012 22:49:08 -0700
parents 27a2167663dd
children 2a9c6eed0c70
files parser.js runtime/progfoot.inc samples/fib.tp samples/fibnof.tp tpc.js
diffstat 5 files changed, 17 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/parser.js	Thu Jul 12 22:10:58 2012 -0700
+++ b/parser.js	Thu Jul 12 22:49:08 2012 -0700
@@ -81,7 +81,9 @@
 'symbol = chars:[a-zA-Z_!?@]+ trailing:(":"? [a-zA-Z_!?@0-9])* ! ":" { for (var i in trailing) { trailing[i] = trailing[i].join(""); } return new symbol(chars.join("") + trailing.join("")); };' +
 'float = digits:[0-9]+ "." decimals:[0-9]+ { return new floatlit(parseFloat(digits.join("") + "." + decimals.join(""))); };' +
 'int = digits:[0-9]+ { return new intlit(parseInt(digits.join(""), 10)); };' +
-'string = "\\"" text:[^\\"]* "\\"" { return new strlit(text.join("")); };' +
+'string = "\\"" text:(strpart/escape)* "\\"" { return new strlit(text.join("")); };' +
+'strpart = text:[^\\"\\\\]+ { return text.join(""); };' + 
+'escape = "\\\\" char:[nt\\"r] { if (char == "n") { return "\\n"; } if (char == "r") { return "\\r"; } return char; };' +
 'object = "#{" ws messages:(assignment / funcall)* "}" { return new object(messages); };' +
 'array = "#[" ws els:opexpr* "]" { return new arraylit(els); };' +
 'list = "[" ws els:opexpr* "]" { return new listlit(els); };' +
--- a/runtime/progfoot.inc	Thu Jul 12 22:10:58 2012 -0700
+++ b/runtime/progfoot.inc	Thu Jul 12 22:49:08 2012 -0700
@@ -23,16 +23,7 @@
 	object * ret = mcall(METHOD_ID_MAIN, 1, mainModule());
 	if (ret->meta == &obj_int32_meta) {
 		obj_int32 * reti32 = (obj_int32 *) ret;
-		printf("%d\n", reti32->num);
-	} else if(ret->meta == &lambda_meta) {
-		puts("returned lambda????");
-	} else {
-		int i = 0;
-		for(; i < 16; ++i) {
-			if (ret->meta->meth_lookup[i] != &no_impl) {
-				printf("slot %d is set\n", i);
-			}
-		}
+		return reti32->num;
 	}
 	return 0;
 }
--- a/samples/fib.tp	Thu Jul 12 22:10:58 2012 -0700
+++ b/samples/fib.tp	Thu Jul 12 22:49:08 2012 -0700
@@ -1,9 +1,4 @@
 #{
-
-console <- foreign: #{
-	log <- foreign: :val {}
-}
-
 true <- #{
   if:else <- :self trueblock :elseblock {
     trueblock:
@@ -25,7 +20,7 @@
 }
 
 main <- {
-  console log: (fib: 30)
+  print: (string: (fib: 30)) . "\n"
 }
 
 }
--- a/samples/fibnof.tp	Thu Jul 12 22:10:58 2012 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-#{
-
-true <- #{
-  if:else <- :self trueblock :elseblock {
-    trueblock:
-  }
-}
-
-false <- #{
-  if:else <- :self trueblock :elseblock {
-    elseblock:
-  }
-}
-
-fib <- :n {
-  if: n < 2 {
-    1
-  } else: {
-    (fib: n-1) + (fib: n-2)
-  }
-}
-
-main <- {
-  fib: 30
-}
-
-}
--- a/tpc.js	Thu Jul 12 22:10:58 2012 -0700
+++ b/tpc.js	Thu Jul 12 22:49:08 2012 -0700
@@ -20,26 +20,19 @@
 		var parsed = parser.parse(text);
 		var c = parsed.toCModule();
 		print(c);
-	} catch(error) {
-		if (error.name == 'SyntaxError') {
-			print('SyntaxError on at', error.line, ',', error.column, ':', error.message);
-			var lines = text.split('\n');
-			print(lines[error.line-1]);
-			var spacer = '';
-			for (var i = 1; i < error.column; i++) {
-				if (lines[error.line-1].charAt(i-1) == '\t') {
-					spacer += '    ';
-				} else {
-					spacer += ' ';
-				}
-			}
-			print(spacer + '^');
-		} else {
-			print("Exception:");
-			var keys = Object.keys(error);
-			for (var i = 0; i < keys.length; ++i) {
-				print('\t', keys[i], error[keys[i]]);
+	} catch(error if error.name == 'SyntaxError') {
+		print('SyntaxError on at', error.line, ',', error.column, ':', error.message);
+		var lines = text.split('\n');
+		print(lines[error.line-1]);
+		var spacer = '';
+		for (var i = 1; i < error.column; i++) {
+			if (lines[error.line-1].charAt(i-1) == '\t') {
+				spacer += '    ';
+			} else {
+				spacer += ' ';
 			}
 		}
+		print(spacer + '^');
+
 	}
 }