Skip to content

Commit

Permalink
Support positive unary operator (do nothing)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 27, 2019
1 parent 9074403 commit fd56bb3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/bytecode/expression_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (g *Generator) compilePrefixExpression(is *InstructionSet, exp *ast.PrefixE
is.define(PutObject, exp.Line(), 0)
g.compileExpression(is, exp.Right, scope, table)
is.define(Send, exp.Line(), exp.Operator, 1, "", &ArgSet{})
case "+":
g.compileExpression(is, exp.Right, scope, table)
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.False, p.parseBooleanLiteral)
p.registerPrefix(token.Null, p.parseNilExpression)
p.registerPrefix(token.Minus, p.parsePrefixExpression)
p.registerPrefix(token.Plus, p.parsePrefixExpression)
p.registerPrefix(token.Asterisk, p.parsePrefixExpression)
p.registerPrefix(token.Bang, p.parsePrefixExpression)
p.registerPrefix(token.LParen, p.parseGroupedExpression)
Expand Down
20 changes: 20 additions & 0 deletions vm/evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,26 @@ func TestMinusPrefixMethodCall(t *testing.T) {
}
}

func TestPlusPrefixMethodCall(t *testing.T) {
tests := []struct {
input string
expected int
}{
{"+5", 5},
{"+10", 10},
{"+(-10)", -10},
{"+(-5)", -5},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

func TestMultiVarAssignment(t *testing.T) {
tests := []struct {
input string
Expand Down

0 comments on commit fd56bb3

Please sign in to comment.