mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-07-02 16:11:49 +00:00
Initial commit
This commit is contained in:
56
tests/binaryen.ts
Normal file
56
tests/binaryen.ts
Normal file
@ -0,0 +1,56 @@
|
||||
/// <reference path="../src/binaryen.d.ts" />
|
||||
import "../src/glue/js";
|
||||
import { Type, Module, MemorySegment, BinaryOp } from "../src/binaryen";
|
||||
import { Target } from "../src/compiler";
|
||||
import { U64 } from "../src/util";
|
||||
|
||||
const mod = Module.create();
|
||||
|
||||
mod.setMemory(1, Module.MAX_MEMORY_WASM32, [
|
||||
MemorySegment.create(new Uint8Array(4), U64.fromI32(8)),
|
||||
MemorySegment.create(new Uint8Array(4), U64.fromI32(12))
|
||||
], Target.WASM32, "memory");
|
||||
|
||||
const add = mod.addFunctionType("iii", Type.I32, [ Type.I32, Type.I32 ]);
|
||||
mod.addFunction("add", add, [], mod.createReturn(
|
||||
mod.createBinary(BinaryOp.AddI32,
|
||||
mod.createGetLocal(0, Type.I32),
|
||||
mod.createGetLocal(1, Type.I32)
|
||||
)
|
||||
));
|
||||
mod.addExport("add", "add");
|
||||
|
||||
const lit = mod.addFunctionType("I", Type.I64, []);
|
||||
mod.addFunction("lit", lit, [], mod.createReturn(
|
||||
mod.createI64(0, 0x80000000) // I64_MIN
|
||||
));
|
||||
mod.addExport("lit", "lit");
|
||||
|
||||
mod.addGlobal("42", Type.I32, false, mod.createI32(42));
|
||||
|
||||
const aSwitch = mod.addFunctionType("i", Type.I32, []);
|
||||
mod.addFunction("aSwitch", aSwitch, [], mod.createBlock("", [
|
||||
mod.createBlock("a", [
|
||||
mod.createBlock("b", [
|
||||
mod.createBlock("c", [
|
||||
mod.createBlock("d", [
|
||||
mod.createSwitch(
|
||||
["a", "b", "c"],
|
||||
"d",
|
||||
mod.createI32(4)
|
||||
)
|
||||
]),
|
||||
mod.createReturn(mod.createI32(3))
|
||||
]),
|
||||
mod.createReturn(mod.createI32(2))
|
||||
]),
|
||||
mod.createReturn(mod.createI32(1))
|
||||
]),
|
||||
mod.createReturn(mod.createI32(0))
|
||||
]));
|
||||
mod.addExport("aSwitch", "aSwitch");
|
||||
|
||||
// mod.optimize();
|
||||
if (mod.validate())
|
||||
_BinaryenModulePrint(mod.ref);
|
||||
_BinaryenModuleDispose(mod.ref);
|
37
tests/i64.ts
Normal file
37
tests/i64.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { I64 } from "../src/util/i64";
|
||||
import * as Long from "long";
|
||||
import * as assert from "assert";
|
||||
|
||||
function test(fn, lo, hi, otherLo, otherHi) {
|
||||
let expected = Long.fromBits(lo, hi)[fn](Long.fromBits(otherLo, otherHi));
|
||||
let actual = new I64(lo, hi); actual[fn + "32"](otherLo, otherHi);
|
||||
assert.equal(actual.lo, expected.low, fn + " lo ");
|
||||
assert.equal(actual.hi, expected.high, fn + " hi");
|
||||
}
|
||||
|
||||
function rand() {
|
||||
let r = Math.random();
|
||||
// 10% edge cases
|
||||
if (r < 0.05) return 0x80000000 | 0;
|
||||
else if (r < 0.1) return 0;
|
||||
return (Math.random() * 0xffffffff) | 0;
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
while (i++ < 1000000) {
|
||||
let lo = rand();
|
||||
let hi = rand();
|
||||
let otherLo = rand();
|
||||
let otherHi = rand();
|
||||
// console.log(lo, hi, otherLo, otherHi);
|
||||
test("add", lo, hi, otherLo, otherHi);
|
||||
test("sub", lo, hi, otherLo, otherHi);
|
||||
test("mul", lo, hi, otherLo, otherHi);
|
||||
test("shl", lo, hi, otherLo, otherHi);
|
||||
test("shr", lo, hi, otherLo, otherHi);
|
||||
test("shru", lo, hi, otherLo, otherHi);
|
||||
test("and", lo, hi, otherLo, otherHi);
|
||||
test("or", lo, hi, otherLo, otherHi);
|
||||
test("xor", lo, hi, otherLo, otherHi);
|
||||
}
|
||||
console.log("done");
|
12
tests/parser/fixtures/class.ts
Normal file
12
tests/parser/fixtures/class.ts
Normal file
@ -0,0 +1,12 @@
|
||||
export class Test<T> {
|
||||
instanceFunction(): void {
|
||||
}
|
||||
static staticFunction(): void {
|
||||
}
|
||||
get instanceGetter(): i32 {
|
||||
}
|
||||
static set staticSetter(v: i32): i32 {
|
||||
}
|
||||
instanceField: i32;
|
||||
static staticField: i32;
|
||||
}
|
10
tests/parser/fixtures/enum.ts
Normal file
10
tests/parser/fixtures/enum.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export const enum A {
|
||||
B = 1,
|
||||
C,
|
||||
D = 3
|
||||
}
|
||||
enum E {
|
||||
F,
|
||||
G = 1 + 2,
|
||||
H = 3 * 4
|
||||
}
|
4
tests/parser/fixtures/function.ts
Normal file
4
tests/parser/fixtures/function.ts
Normal file
@ -0,0 +1,4 @@
|
||||
function simple(): void {
|
||||
}
|
||||
function typeparams<T, V extends T>(a: V | null = null): void {
|
||||
}
|
171
tests/parser/fixtures/precedence.tree.ts
Normal file
171
tests/parser/fixtures/precedence.tree.ts
Normal file
@ -0,0 +1,171 @@
|
||||
// incrementing precedence
|
||||
a
|
||||
=
|
||||
b
|
||||
?
|
||||
X
|
||||
:
|
||||
c
|
||||
||
|
||||
d
|
||||
&&
|
||||
e
|
||||
|
|
||||
f
|
||||
^
|
||||
g
|
||||
&
|
||||
h
|
||||
==
|
||||
i
|
||||
<
|
||||
j
|
||||
<<
|
||||
k
|
||||
+
|
||||
l
|
||||
/
|
||||
++
|
||||
m
|
||||
++
|
||||
;
|
||||
// decrementing precedence
|
||||
++
|
||||
a
|
||||
++
|
||||
/
|
||||
b
|
||||
+
|
||||
c
|
||||
<<
|
||||
d
|
||||
<
|
||||
e
|
||||
==
|
||||
f
|
||||
&
|
||||
g
|
||||
^
|
||||
h
|
||||
|
|
||||
i
|
||||
&&
|
||||
j
|
||||
||
|
||||
k
|
||||
?
|
||||
X
|
||||
:
|
||||
y
|
||||
=
|
||||
l
|
||||
;
|
||||
// assignment (right-to-left)
|
||||
a
|
||||
=
|
||||
b
|
||||
+=
|
||||
c
|
||||
-=
|
||||
d
|
||||
**=
|
||||
e
|
||||
*=
|
||||
f
|
||||
/=
|
||||
g
|
||||
%=
|
||||
h
|
||||
<<=
|
||||
i
|
||||
>>=
|
||||
j
|
||||
>>>=
|
||||
k
|
||||
&=
|
||||
l
|
||||
^=
|
||||
m
|
||||
|=
|
||||
n
|
||||
;
|
||||
// equality (left-to-right)
|
||||
a
|
||||
==
|
||||
b
|
||||
!=
|
||||
c
|
||||
===
|
||||
d
|
||||
!==
|
||||
e
|
||||
;
|
||||
// relational (left-to-right)
|
||||
a
|
||||
<
|
||||
b
|
||||
<=
|
||||
c
|
||||
>
|
||||
d
|
||||
>=
|
||||
e
|
||||
;
|
||||
// bitwise shift (left-to-right)
|
||||
a
|
||||
<<
|
||||
b
|
||||
>>
|
||||
c
|
||||
>>>
|
||||
d
|
||||
;
|
||||
// addition (left-to-right)
|
||||
a
|
||||
+
|
||||
b
|
||||
-
|
||||
c
|
||||
;
|
||||
// exponentiation (right-to-left), multiplication (left-to-right)
|
||||
a
|
||||
**
|
||||
b
|
||||
*
|
||||
c
|
||||
/
|
||||
d
|
||||
%
|
||||
e
|
||||
**
|
||||
f
|
||||
*
|
||||
g
|
||||
;
|
||||
// prefix (right-to-left)
|
||||
delete
|
||||
void
|
||||
typeof
|
||||
--
|
||||
++
|
||||
-
|
||||
+
|
||||
~
|
||||
!
|
||||
a
|
||||
;
|
||||
// parenthesized
|
||||
(
|
||||
a
|
||||
+
|
||||
(
|
||||
b
|
||||
/
|
||||
c
|
||||
)
|
||||
-
|
||||
d
|
||||
)
|
||||
*
|
||||
e
|
||||
;
|
46
tests/parser/index.ts
Normal file
46
tests/parser/index.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import * as fs from "fs";
|
||||
import * as diff from "diff";
|
||||
import * as chalk from "chalk";
|
||||
|
||||
import "../../src/glue/js";
|
||||
import { NodeKind, ExpressionStatement } from "../../src/ast";
|
||||
import { Parser } from "../../src/parser";
|
||||
|
||||
const files = fs.readdirSync(__dirname + "/fixtures");
|
||||
files.forEach(filename => {
|
||||
if (filename.charAt(0) == "_") return;
|
||||
const isTree = filename.indexOf(".tree.") >= 0;
|
||||
const parser = new Parser();
|
||||
const text = fs.readFileSync(__dirname + "/fixtures/" + filename, { encoding: "utf8" }).replace(/\r?\n/g, "\n").replace(/^\/\/.*\r?\n/mg, "");
|
||||
parser.parseFile(text, filename, true);
|
||||
var sb: string[] = [];
|
||||
if (isTree) {
|
||||
const statements = parser.program.sources[0].statements;
|
||||
statements.forEach(stmt => {
|
||||
if (stmt.kind != NodeKind.EXPRESSION) return;
|
||||
(<ExpressionStatement>stmt).expression.serializeAsTree(sb, 0);
|
||||
sb.push(";\n");
|
||||
});
|
||||
} else
|
||||
parser.program.sources[0].serialize(sb);
|
||||
const actual = sb.join("");
|
||||
const expected = isTree ? text : text.replace(/^\s+/mg, "");
|
||||
const diffs = diff.diffLines(expected, actual);
|
||||
let changed = false;
|
||||
diffs.forEach(part => {
|
||||
if (part.added || part.removed)
|
||||
changed = true;
|
||||
});
|
||||
if (changed) { // print it
|
||||
console.log("Differences in " + filename + ":");
|
||||
diffs.forEach(part => {
|
||||
if (part.added || part.removed)
|
||||
changed = true;
|
||||
process.stderr.write((part.added ? chalk.green : part.removed ? chalk.red : chalk.grey)(part.value));
|
||||
});
|
||||
} else {
|
||||
console.log("No differences in " + filename + ".");
|
||||
}
|
||||
// parser.program.initialize();
|
||||
// console.log(parser.program.names);
|
||||
});
|
8
tests/path.ts
Normal file
8
tests/path.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { normalizePath, resolvePath } from "../src/util";
|
||||
import * as path from "path";
|
||||
|
||||
let test = "./Y/./N/./N/../../././../Y/./.";
|
||||
console.log(normalizePath(test));
|
||||
console.log(path.posix.normalize(test));
|
||||
|
||||
console.log(resolvePath("../../..", "lib/util/i64.ts"));
|
23
tests/tokenizer.ts
Normal file
23
tests/tokenizer.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import "../src/glue/js";
|
||||
import { Tokenizer, Token } from "../src/tokenizer";
|
||||
import { Source } from "../src/reflection";
|
||||
import * as fs from "fs";
|
||||
|
||||
const text = fs.readFileSync(__dirname + "/../src/tokenizer.ts").toString();
|
||||
|
||||
const tn = new Tokenizer(new Source("tokenizer.ts", text));
|
||||
|
||||
let token;
|
||||
do {
|
||||
token = tn.next();
|
||||
let range = tn.range();
|
||||
console.log(Token[token] + " -> " + range.source.text.substring(range.start, range.end));
|
||||
if (token == Token.IDENTIFIER)
|
||||
console.log("> " + tn.readIdentifier());
|
||||
else if (token == Token.INTEGERLITERAL)
|
||||
console.log("> " + tn.readInteger());
|
||||
else if (token == Token.FLOATLITERAL)
|
||||
console.log("> " + tn.readFloat());
|
||||
else if (token == Token.STRINGLITERAL)
|
||||
console.log("> " + tn.readString());
|
||||
} while (token != Token.ENDOFFILE);
|
Reference in New Issue
Block a user