Add pre/postfix expressions
This commit is contained in:
parent
0236dfbb2d
commit
e9074e1632
@ -350,8 +350,9 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (string, error) {
|
|||||||
}
|
}
|
||||||
case UnaryOperation_LogicalNot:
|
case UnaryOperation_LogicalNot:
|
||||||
return wat + "i32.eqz\n", nil
|
return wat + "i32.eqz\n", nil
|
||||||
|
case UnaryOperation_PreIncrement, UnaryOperation_PreDecrement, UnaryOperation_PostIncrement, UnaryOperation_PostDecrement:
|
||||||
|
// TODO: implement
|
||||||
}
|
}
|
||||||
|
|
||||||
case Expression_Cast:
|
case Expression_Cast:
|
||||||
cast := expr.Value.(CastExpression)
|
cast := expr.Value.(CastExpression)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ u64 fib(u64 i) {
|
|||||||
u64 fibA = 0u64;
|
u64 fibA = 0u64;
|
||||||
u64 fibB = 1u64;
|
u64 fibB = 1u64;
|
||||||
|
|
||||||
while((i -= 1u64) > 0u64) {
|
while(--i > 0u64) {
|
||||||
u64 tmp = fibB;
|
u64 tmp = fibB;
|
||||||
fibB = fibA + fibB;
|
fibB = fibA + fibB;
|
||||||
fibA = tmp;
|
fibA = tmp;
|
||||||
|
42
parser.go
42
parser.go
@ -136,6 +136,10 @@ const (
|
|||||||
UnaryOperation_Nop
|
UnaryOperation_Nop
|
||||||
UnaryOperation_BitwiseNot
|
UnaryOperation_BitwiseNot
|
||||||
UnaryOperation_LogicalNot
|
UnaryOperation_LogicalNot
|
||||||
|
UnaryOperation_PreIncrement
|
||||||
|
UnaryOperation_PreDecrement
|
||||||
|
UnaryOperation_PostIncrement
|
||||||
|
UnaryOperation_PostDecrement
|
||||||
)
|
)
|
||||||
|
|
||||||
type BinaryExpression struct {
|
type BinaryExpression struct {
|
||||||
@ -603,7 +607,7 @@ func (p *Parser) tryUnaryExpression() (*Expression, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
op, err := pCopy.tryOperator(Operator_Minus, Operator_Plus, Operator_BitwiseNot, Operator_Not)
|
op, err := pCopy.tryOperator(Operator_Minus, Operator_Plus, Operator_BitwiseNot, Operator_Not, Operator_PlusPlus, Operator_MinusMinus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -619,12 +623,44 @@ func (p *Parser) tryUnaryExpression() (*Expression, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
*p = pCopy
|
*p = pCopy
|
||||||
|
switch *op {
|
||||||
|
case Operator_PlusPlus:
|
||||||
|
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: UnaryOperation_PreIncrement, Value: *expr}, Position: token.Position}, nil
|
||||||
|
case Operator_MinusMinus:
|
||||||
|
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: UnaryOperation_PreDecrement, Value: *expr}, Position: token.Position}, nil
|
||||||
|
}
|
||||||
|
|
||||||
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: getUnaryOperation(*op), Value: *expr}, Position: token.Position}, nil
|
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: getUnaryOperation(*op), Value: *expr}, Position: token.Position}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: pre-/postfix in-/decrement expr
|
expr, err := pCopy.tryPrimaryExpression()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return p.tryPrimaryExpression()
|
if expr == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
op, err = pCopy.tryOperator(Operator_PlusPlus, Operator_MinusMinus)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if op == nil {
|
||||||
|
*p = pCopy
|
||||||
|
return expr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
*p = pCopy
|
||||||
|
switch *op {
|
||||||
|
case Operator_PlusPlus:
|
||||||
|
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: UnaryOperation_PostIncrement, Value: *expr}, Position: token.Position}, nil
|
||||||
|
case Operator_MinusMinus:
|
||||||
|
return &Expression{Type: Expression_Unary, Value: UnaryExpression{Operation: UnaryOperation_PostDecrement, Value: *expr}, Position: token.Position}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Operator not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) tryBinaryExpression0(opFunc func() (*Expression, error), operators ...Operator) (*Expression, error) {
|
func (p *Parser) tryBinaryExpression0(opFunc func() (*Expression, error), operators ...Operator) (*Expression, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user