mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
Retain wrap state in parenthesized expressions; Void statements fwiw
This commit is contained in:
parent
00c4f6fa52
commit
d81ce5f907
2
dist/assemblyscript.js
vendored
2
dist/assemblyscript.js
vendored
File diff suppressed because one or more lines are too long
2
dist/assemblyscript.js.map
vendored
2
dist/assemblyscript.js.map
vendored
File diff suppressed because one or more lines are too long
19
src/ast.ts
19
src/ast.ts
@ -66,6 +66,7 @@ export enum NodeKind {
|
||||
THROW,
|
||||
TRY,
|
||||
VARIABLE,
|
||||
VOID,
|
||||
WHILE,
|
||||
|
||||
// declaration statements
|
||||
@ -897,6 +898,16 @@ export abstract class Node {
|
||||
return elem;
|
||||
}
|
||||
|
||||
static createVoidStatement(
|
||||
expression: Expression,
|
||||
range: Range
|
||||
): VoidStatement {
|
||||
var stmt = new VoidStatement();
|
||||
stmt.range = range;
|
||||
stmt.expression = expression;
|
||||
return stmt;
|
||||
}
|
||||
|
||||
static createWhileStatement(
|
||||
condition: Expression,
|
||||
statement: Statement,
|
||||
@ -1668,6 +1679,14 @@ export class VariableStatement extends Statement {
|
||||
declarations: VariableDeclaration[];
|
||||
}
|
||||
|
||||
/** Represents a void statement dropping an expression's value. */
|
||||
export class VoidStatement extends Statement {
|
||||
kind = NodeKind.VOID;
|
||||
|
||||
/** Expression being dropped. */
|
||||
expression: Expression;
|
||||
}
|
||||
|
||||
/** Represents a `while` statement. */
|
||||
export class WhileStatement extends Statement {
|
||||
kind = NodeKind.WHILE;
|
||||
|
@ -79,6 +79,7 @@ import {
|
||||
TryStatement,
|
||||
VariableDeclaration,
|
||||
VariableStatement,
|
||||
VoidStatement,
|
||||
WhileStatement,
|
||||
|
||||
Expression,
|
||||
@ -1195,9 +1196,14 @@ export class Compiler extends DiagnosticEmitter {
|
||||
expr = this.compileTryStatement(<TryStatement>statement);
|
||||
break;
|
||||
|
||||
case NodeKind.VARIABLE:
|
||||
var variableInit = this.compileVariableStatement(<VariableStatement>statement);
|
||||
expr = variableInit ? variableInit : this.module.createNop();
|
||||
case NodeKind.VARIABLE: {
|
||||
let initializer = this.compileVariableStatement(<VariableStatement>statement);
|
||||
expr = initializer ? initializer : this.module.createNop();
|
||||
break;
|
||||
}
|
||||
|
||||
case NodeKind.VOID:
|
||||
expr = this.compileVoidStatement(<VoidStatement>statement);
|
||||
break;
|
||||
|
||||
case NodeKind.WHILE:
|
||||
@ -1646,6 +1652,10 @@ export class Compiler extends DiagnosticEmitter {
|
||||
: 0;
|
||||
}
|
||||
|
||||
compileVoidStatement(statement: VoidStatement): ExpressionRef {
|
||||
return this.compileExpression(statement.expression, Type.void, ConversionKind.EXPLICIT, false);
|
||||
}
|
||||
|
||||
compileWhileStatement(statement: WhileStatement): ExpressionRef {
|
||||
|
||||
// The condition does not yet initialize a branch
|
||||
@ -1826,7 +1836,11 @@ export class Compiler extends DiagnosticEmitter {
|
||||
break;
|
||||
|
||||
case NodeKind.PARENTHESIZED:
|
||||
expr = this.compileParenthesizedExpression(<ParenthesizedExpression>expression, contextualType);
|
||||
expr = this.compileParenthesizedExpression(
|
||||
<ParenthesizedExpression>expression,
|
||||
contextualType,
|
||||
wrapSmallIntegers
|
||||
);
|
||||
break;
|
||||
|
||||
case NodeKind.PROPERTYACCESS:
|
||||
@ -4507,9 +4521,18 @@ export class Compiler extends DiagnosticEmitter {
|
||||
return this.module.createUnreachable();
|
||||
}
|
||||
|
||||
compileParenthesizedExpression(expression: ParenthesizedExpression, contextualType: Type): ExpressionRef {
|
||||
compileParenthesizedExpression(
|
||||
expression: ParenthesizedExpression,
|
||||
contextualType: Type,
|
||||
wrapSmallIntegers: bool = true
|
||||
): ExpressionRef {
|
||||
// does not change types, just order
|
||||
return this.compileExpression(expression.expression, contextualType, ConversionKind.NONE);
|
||||
return this.compileExpression(
|
||||
expression.expression,
|
||||
contextualType,
|
||||
ConversionKind.NONE,
|
||||
wrapSmallIntegers
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,6 +75,7 @@ import {
|
||||
TypeParameter,
|
||||
VariableStatement,
|
||||
VariableDeclaration,
|
||||
VoidStatement,
|
||||
WhileStatement,
|
||||
|
||||
addModifier,
|
||||
@ -1724,6 +1725,9 @@ export class Parser extends DiagnosticEmitter {
|
||||
case Token.TYPE:
|
||||
return this.parseTypeDeclaration(tn, null);
|
||||
|
||||
case Token.VOID:
|
||||
return this.parseVoidStatement(tn);
|
||||
|
||||
case Token.WHILE:
|
||||
return this.parseWhileStatement(tn);
|
||||
|
||||
@ -2196,6 +2200,20 @@ export class Parser extends DiagnosticEmitter {
|
||||
return null;
|
||||
}
|
||||
|
||||
parseVoidStatement(
|
||||
tn: Tokenizer
|
||||
): VoidStatement | null {
|
||||
|
||||
// at 'void': Expression ';'?
|
||||
|
||||
var startPos = tn.tokenPos;
|
||||
var expression = this.parseExpression(tn, Precedence.GROUPING);
|
||||
if (!expression) return null;
|
||||
var ret = Node.createVoidStatement(expression, tn.range(startPos, tn.pos));
|
||||
tn.skip(Token.SEMICOLON);
|
||||
return ret;
|
||||
}
|
||||
|
||||
parseWhileStatement(
|
||||
tn: Tokenizer
|
||||
): WhileStatement | null {
|
||||
@ -2209,7 +2227,7 @@ export class Parser extends DiagnosticEmitter {
|
||||
if (tn.skip(Token.CLOSEPAREN)) {
|
||||
var statement = this.parseStatement(tn);
|
||||
if (!statement) return null;
|
||||
var ret = Node.createWhileStatement(<Expression>expression, <Statement>statement, tn.range(startPos, tn.pos));
|
||||
var ret = Node.createWhileStatement(expression, statement, tn.range(startPos, tn.pos));
|
||||
tn.skip(Token.SEMICOLON);
|
||||
return ret;
|
||||
} else {
|
||||
|
@ -153,22 +153,13 @@
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.or
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 24)
|
||||
)
|
||||
(i32.const 16)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const -256)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(get_local $0)
|
||||
|
@ -74,20 +74,13 @@
|
||||
(i32.and
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.and
|
||||
(i32.and
|
||||
(i32.and
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 65535)
|
||||
)
|
||||
(i32.const 65280)
|
||||
)
|
||||
(i32.const 65535)
|
||||
)
|
||||
(i32.and
|
||||
(i32.and
|
||||
(i32.shr_u
|
||||
(get_local $0)
|
||||
@ -95,10 +88,7 @@
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
(i32.const 65535)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.and
|
||||
@ -108,8 +98,6 @@
|
||||
(i32.const 65535)
|
||||
)
|
||||
)
|
||||
(i32.const 65535)
|
||||
)
|
||||
)
|
||||
(i32.const 65535)
|
||||
)
|
||||
@ -352,19 +340,11 @@
|
||||
(i32.shl
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.const 65280)
|
||||
@ -373,12 +353,6 @@
|
||||
(i32.const 16)
|
||||
)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(get_local $0)
|
||||
@ -386,13 +360,7 @@
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
)
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.shr_s
|
||||
@ -405,10 +373,6 @@
|
||||
(i32.const 16)
|
||||
)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
|
15
tests/compiler/void.optimized.wat
Normal file
15
tests/compiler/void.optimized.wat
Normal file
@ -0,0 +1,15 @@
|
||||
(module
|
||||
(type $i (func (result i32)))
|
||||
(type $v (func))
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $void/anInt (; 0 ;) (type $i) (result i32)
|
||||
(i32.const 2)
|
||||
)
|
||||
(func $start (; 1 ;) (type $v)
|
||||
(drop
|
||||
(call $void/anInt)
|
||||
)
|
||||
)
|
||||
)
|
12
tests/compiler/void.ts
Normal file
12
tests/compiler/void.ts
Normal file
@ -0,0 +1,12 @@
|
||||
void 1;
|
||||
|
||||
function anInt(): i32 {
|
||||
return 2;
|
||||
}
|
||||
|
||||
void anInt();
|
||||
|
||||
var u8Val1: u8 = 1;
|
||||
var u8Val2: u8 = 255;
|
||||
|
||||
void (u8Val1 + u8Val2); // can remain unwrapped
|
29
tests/compiler/void.untouched.wat
Normal file
29
tests/compiler/void.untouched.wat
Normal file
@ -0,0 +1,29 @@
|
||||
(module
|
||||
(type $i (func (result i32)))
|
||||
(type $v (func))
|
||||
(global $void/u8Val1 (mut i32) (i32.const 1))
|
||||
(global $void/u8Val2 (mut i32) (i32.const 255))
|
||||
(global $HEAP_BASE i32 (i32.const 4))
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $void/anInt (; 0 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 2)
|
||||
)
|
||||
)
|
||||
(func $start (; 1 ;) (type $v)
|
||||
(drop
|
||||
(i32.const 1)
|
||||
)
|
||||
(drop
|
||||
(call $void/anInt)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(get_global $void/u8Val1)
|
||||
(get_global $void/u8Val2)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user