Skip to content
Browse files

Added mocha test support.

  • Loading branch information...
1 parent 5cf3908 commit 80652b8057a2912b79e000b4fbac28c2019bb802 @mbebenita committed
Showing with 45 additions and 50 deletions.
  1. +3 −3 docs/index.html
  2. +1 −1 src/compiler.js
  3. +13 −4 src/ljc.js
  4. +0 −31 src/ply.html
  5. +0 −11 src/ply.js
  6. +28 −0 test/array-0.ljs
View
6 docs/index.html
@@ -84,7 +84,7 @@
let int x = 3;
let int y = 2;
-/* Arithmetic follows C semantics. Arithmetic on integers begets
+/* Arithmetic follows C semantics. Arithmetic on integers begets
integers, truncated with | 0. */
trace("Result is an integer: " + x / y);
trace("Integral literals are typed as integers: " + 1 / 2);
@@ -482,7 +482,7 @@
}
var source = this.getValue();
source = 'extern trace;\n' + source;
- result.innerHTML = LJC.compile(name, name, source, { bare: true, memcheck: false });
+ result.innerHTML = LJC.compile(source, { bare: true, memcheck: false, filename: name });
} catch (x) {
result.innerHTML = x.message;
if (x.lineNumber) {
@@ -548,7 +548,7 @@
} else {
source = 'extern trace;\n' + source;
}
- var code = LJC.compile("ex" + number, "ex" + number, source, { memcheck: !!enableMemCheck });
+ var code = LJC.compile(source, { memcheck: !!enableMemCheck, filename: "ex" + number});
result.innerHTML += "Compiled in: " + (new Date() - start) + " ms\n";
result.innerHTML += "-----------------------------------------------------\n";
View
2 src/compiler.js
@@ -1434,7 +1434,7 @@
}
body.push(new VariableDeclaration("const", [mdecl]));
// todo: broken just like above
- if(name !== "memory") {
+ if (name !== "memory") {
assert (memcheck !== undefined);
body.push(new ExpressionStatement(
new CallExpression(new MemberExpression(cachedMEMORY, new Identifier("set_memcheck")),
View
17 src/ljc.js
@@ -7,6 +7,13 @@
var mode;
if (typeof process !== "undefined") {
mode = NODE_JS;
+ // Install compiler as an extension for '.ljs' files that are loaded using the
+ // |require| function. This is how mocha tests are executed.
+ var fs = require('fs');
+ require.extensions['.ljs'] = function(module, filename) {
+ var source = fs.readFileSync(filename, 'utf8');
+ return module._compile(compile(source, {filename: filename, memcheck: false}), filename);
+ };
} else if (typeof snarf !== "undefined") {
mode = JS_SHELL;
} else {
@@ -143,7 +150,9 @@
basename = basename.substr(0, basename.lastIndexOf(".")) || basename;
var source = snarf(filename);
- var code = compile(basename, filename, source, options);
+ options.filename = filename;
+ options.basename = basename;
+ var code = compile(source, options);
if (options["pretty-print"]) {
print(pretty(code));
@@ -167,7 +176,7 @@
}
}
- function compile(name, logName, source, options) {
+ function compile(source, options) {
// -W anything infers -W.
for (var p in options) {
if (p.charAt(0) === "W") {
@@ -180,7 +189,7 @@
options.warn = false;
}
- var logger = new util.Logger("ljc", logName, source, options);
+ var logger = new util.Logger("ljc", options.filename, source, options);
var code;
try {
@@ -191,7 +200,7 @@
if (options["only-parse"]) {
code = node;
} else {
- node = compiler.compile(node, name, logger, options);
+ node = compiler.compile(node, options.filename, logger, options);
if (options["emit-ast"]) {
code = node;
} else {
View
31 src/ply.html
@@ -1,31 +0,0 @@
-
-
-<html>
- <head>
- <title>JS*</title>
- </head>
- <script src="util.js"></script>
- <script src="modules.js"></script>
- <script src="memory.js"></script>
- <script>
- print = function (x) {
- console.info(x);
- }
-
- function trace(s) {
- print(s);
- }
- </script>
- <body>
- <script>
- var memory = require("memory");
-
-
- trace(memory.malloc(123));
- trace(memory.malloc(123));
- trace(memory.malloc(123));
-
- </script>
- </body>
-</html>
-
View
11 src/ply.js
@@ -1,11 +0,0 @@
-function trace(s) {
- print(s);
-}
-
-load("util.js")
-load("modules.js")
-load("memory.js")
-
-var memory = require("memory");
-
-trace(memory.malloc(123));
View
28 test/array-0.ljs
@@ -0,0 +1,28 @@
+extern describe, it;
+extern Number;
+
+let assert = require("assert");
+
+describe('Array 0', function() {
+ function testElements(int * a, int length) {
+ for (let int i = 0; i < length; i++) {
+ a[i] = i;
+ }
+ let int sum = 0;
+ for (let int i = 0; i < length; i++) {
+ sum += a[i];
+ }
+ sum.should.equal((length * (length - 1)) / 2);
+ }
+
+ it('allocates an array on the heap', function () {
+ testElements(new int [1024], 1024);
+ });
+
+ it('allocates an array on the stack', function () {
+ let int a [1024];
+ testElements(a, 1024);
+ });
+});
+
+

0 comments on commit 80652b8

Please sign in to comment.
Something went wrong with that request. Please try again.