Skip to content

Commit b9edfb5

Browse files
committed
More tests and fixes (unary, binary, globals)
1 parent ef85993 commit b9edfb5

22 files changed

+2160
-81
lines changed

src/ast.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,13 @@ export abstract class Statement extends Node {
925925
return stmt;
926926
}
927927

928-
static createVariableDeclaration(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, range: Range): VariableDeclaration {
928+
static createVariableDeclaration(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, modifiers: Modifier[] | null, range: Range): VariableDeclaration {
929929
const elem: VariableDeclaration = new VariableDeclaration();
930930
elem.range = range;
931931
(elem.identifier = identifier).parent = elem;
932932
if (elem.type = type) (<TypeNode>type).parent = elem;
933933
if (elem.initializer = initializer) (<Expression>initializer).parent = elem;
934+
elem.modifiers = modifiers;
934935
return elem;
935936
}
936937

@@ -1681,7 +1682,7 @@ export class TryStatement extends Statement {
16811682
export class VariableDeclaration extends VariableLikeDeclarationStatement {
16821683

16831684
kind = NodeKind.VARIABLEDECLARATION;
1684-
modifiers = null;
1685+
modifiers: Modifier[] | null;
16851686

16861687
serialize(sb: string[]): void {
16871688
this.identifier.serialize(sb);

src/compiler.ts

Lines changed: 105 additions & 69 deletions
Large diffs are not rendered by default.

src/diagnosticMessages.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export enum DiagnosticCode {
6060
Function_implementation_is_missing_or_not_immediately_following_the_declaration = 2391,
6161
Duplicate_function_implementation = 2393,
6262
Export_declaration_conflicts_with_exported_declaration_of_0 = 2484,
63+
Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property = 2540,
6364
The_target_of_an_assignment_must_be_a_variable_or_a_property_access = 2541,
6465
Expected_0_arguments_but_got_1 = 2554,
6566
Expected_at_least_0_arguments_but_got_1 = 2555,
@@ -128,6 +129,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
128129
case 2391: return "Function implementation is missing or not immediately following the declaration.";
129130
case 2393: return "Duplicate function implementation.";
130131
case 2484: return "Export declaration conflicts with exported declaration of '{0}'.";
132+
case 2540: return "Cannot assign to '{0}' because it is a constant or a read-only property.";
131133
case 2541: return "The target of an assignment must be a variable or a property access.";
132134
case 2554: return "Expected {0} arguments, but got {1}.";
133135
case 2555: return "Expected at least {0} arguments, but got {1}.";

src/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"Function implementation is missing or not immediately following the declaration.": 2391,
6161
"Duplicate function implementation.": 2393,
6262
"Export declaration conflicts with exported declaration of '{0}'.": 2484,
63+
"Cannot assign to '{0}' because it is a constant or a read-only property.": 2540,
6364
"The target of an assignment must be a variable or a property access.": 2541,
6465
"Expected {0} arguments, but got {1}.": 2554,
6566
"Expected at least {0} arguments, but got {1}.": 2555,

src/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export class Parser extends DiagnosticEmitter {
350350
const members: VariableDeclaration[] = new Array();
351351
const isDeclare = hasModifier(ModifierKind.DECLARE, modifiers);
352352
do {
353-
const member: VariableDeclaration | null = this.parseVariableDeclaration(tn, isDeclare);
353+
const member: VariableDeclaration | null = this.parseVariableDeclaration(tn, isDeclare, modifiers);
354354
if (!member)
355355
return null;
356356
members.push(<VariableDeclaration>member);
@@ -361,7 +361,7 @@ export class Parser extends DiagnosticEmitter {
361361
return ret;
362362
}
363363

364-
parseVariableDeclaration(tn: Tokenizer, isDeclare: bool = false): VariableDeclaration | null {
364+
parseVariableDeclaration(tn: Tokenizer, isDeclare: bool = false, parentModifiers: Modifier[]): VariableDeclaration | null {
365365
// Identifier (':' Type)? ('=' Expression)?
366366
if (!tn.skip(Token.IDENTIFIER)) {
367367
this.error(DiagnosticCode.Identifier_expected, tn.range());
@@ -383,7 +383,7 @@ export class Parser extends DiagnosticEmitter {
383383
if (!initializer)
384384
return null;
385385
}
386-
return Statement.createVariableDeclaration(identifier, type, initializer, Range.join(identifier.range, tn.range()));
386+
return Statement.createVariableDeclaration(identifier, type, initializer, parentModifiers, Range.join(identifier.range, tn.range()));
387387
}
388388

389389
parseEnum(tn: Tokenizer, modifiers: Modifier[]): EnumDeclaration | null {

src/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ export class Global extends Element {
764764

765765
get isExport(): bool { return this.declaration ? hasModifier(ModifierKind.EXPORT, this.declaration.modifiers) : /* internals aren't exports */ false; }
766766
get isGlobalExport(): bool { return this.globalExportName != null; }
767-
get isMutable(): bool { return this.declaration ? hasModifier(ModifierKind.CONST, this.declaration.modifiers) : /* internals are immutable */ false; }
767+
get isMutable(): bool { return this.declaration ? !hasModifier(ModifierKind.CONST, this.declaration.modifiers) : /* internals are immutable */ false; }
768768
}
769769

770770
/** A function parameter. */

tests/compiler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,24 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
4949
const actual = module.toText() + "(;\n[program.elements]\n " + iterate(program.elements.keys()).join("\n ") + "\n[program.exports]\n " + iterate(program.exports.keys()).join("\n ") + "\n;)\n";
5050
const fixture = path.basename(filename, ".ts") + ".wast";
5151

52+
if (module.validate())
53+
console.log("Validates");
54+
5255
if (isCreate) {
5356
fs.writeFileSync(__dirname + "/compiler/" + fixture, actual, { encoding: "utf8" });
54-
console.log("Created\n");
57+
console.log("Created");
5558
} else {
5659
const expected = fs.readFileSync(__dirname + "/compiler/" + fixture, { encoding: "utf8" });
5760
const diffs = diff("compiler/" + fixture, expected, actual);
5861
if (diffs !== null) {
5962
process.exitCode = 1;
6063
console.log(diffs);
6164
} else {
62-
console.log("No changes\n");
65+
console.log("No changes");
6366
}
6467
}
68+
69+
console.log();
6570
});
6671

6772
function iterate<T>(it: IterableIterator<T>): T[] {

tests/compiler/binary.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
let b: bool = false;
2+
3+
let i: i32 = 0;
4+
5+
i < 1;
6+
i > 1;
7+
i <= 1;
8+
i >= 1;
9+
i == 1;
10+
i === 1;
11+
i + 1;
12+
i - 1;
13+
i * 1;
14+
i / 1;
15+
i % 1;
16+
i << 1;
17+
i >> 1;
18+
i >>> 1;
19+
i & 1;
20+
i | 1;
21+
i ^ 1;
22+
23+
b = i < 1;
24+
b = i > 1;
25+
b = i <= 1;
26+
b = i >= 1;
27+
b = i == 1;
28+
b = i === 1;
29+
i = i + 1;
30+
i = i - 1;
31+
i = i * 1;
32+
i = i / 1;
33+
i = i % 1;
34+
i = i << 1;
35+
i = i >> 1;
36+
i = i >>> 1;
37+
i = i & 1;
38+
i = i | 1;
39+
i = i ^ 1;
40+
41+
i += 1;
42+
i -= 1;
43+
i *= 1;
44+
i %= 1;
45+
i <<= 1;
46+
i >>= 1;
47+
i >>>= 1;
48+
i &= 1;
49+
i |= 1;
50+
i ^= 1;
51+
52+
let I: i64 = 0;
53+
54+
I < 1;
55+
I > 1;
56+
I <= 1;
57+
I >= 1;
58+
I == 1;
59+
I === 1;
60+
I + 1;
61+
I - 1;
62+
I * 1;
63+
I / 1;
64+
I % 1;
65+
I << 1;
66+
I >> 1;
67+
I >>> 1;
68+
I & 1;
69+
I | 1;
70+
I ^ 1;
71+
72+
b = I < 1;
73+
b = I > 1;
74+
b = I <= 1;
75+
b = I >= 1;
76+
b = I == 1;
77+
b = I === 1;
78+
I = I + 1;
79+
I = I - 1;
80+
I = I * 1;
81+
I = I / 1;
82+
I = I % 1;
83+
I = I << 1;
84+
I = I >> 1;
85+
I = I >>> 1;
86+
I = I & 1;
87+
I = I | 1;
88+
I = I ^ 1;
89+
90+
I += 1;
91+
I -= 1;
92+
I *= 1;
93+
I %= 1;
94+
I <<= 1;
95+
I >>= 1;
96+
I >>>= 1;
97+
I &= 1;
98+
I |= 1;
99+
I ^= 1;
100+
101+
let f: f32 = 0;
102+
103+
f < 1;
104+
f > 1;
105+
f <= 1;
106+
f >= 1;
107+
f == 1;
108+
f === 1;
109+
f + 1;
110+
f - 1;
111+
f * 1;
112+
f / 1;
113+
// f % 1;
114+
115+
b = f < 1;
116+
b = f > 1;
117+
b = f <= 1;
118+
b = f >= 1;
119+
b = f == 1;
120+
b = f === 1;
121+
f = f + 1;
122+
f = f - 1;
123+
f = f * 1;
124+
f = f / 1;
125+
// f = f % 1;
126+
127+
f += 1;
128+
f -= 1;
129+
f *= 1;
130+
// f %= 1;
131+
132+
let F: f64 = 0;
133+
134+
F < 1;
135+
F > 1;
136+
F <= 1;
137+
F >= 1;
138+
F == 1;
139+
F === 1;
140+
F + 1;
141+
F - 1;
142+
F * 1;
143+
F / 1;
144+
// f % 1;
145+
146+
b = F < 1;
147+
b = F > 1;
148+
b = F <= 1;
149+
b = F >= 1;
150+
b = F == 1;
151+
b = F === 1;
152+
F = F + 1;
153+
F = F - 1;
154+
F = F * 1;
155+
F = F / 1;
156+
// F = F % 1;
157+
158+
F += 1;
159+
F -= 1;
160+
F *= 1;
161+
// F %= 1;

0 commit comments

Comments
 (0)