diff tpc.js @ 43:27a2167663dd

Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
author Mike Pavone <pavone@retrodev.com>
date Thu, 12 Jul 2012 22:10:58 -0700
parents 3b0503a67165
children 9dd370530f69
line wrap: on
line diff
--- a/tpc.js	Thu Jul 12 20:14:15 2012 -0700
+++ b/tpc.js	Thu Jul 12 22:10:58 2012 -0700
@@ -16,7 +16,30 @@
 	load('parser.js');
 	load('compiler.js');
 	load('cbackend.js');
-	var parsed = parser.parse(text);
-	var c = parsed.toCModule();
-	print(c);
+	try {
+		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]]);
+			}
+		}
+	}
 }