Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

expression statements that automatically pop after evaluating to prev…

…ent memory leaks
  • Loading branch information...
commit 7eff0be0a78a9974528b87f439d1899476875263 1 parent 38f48a1
@charliesome authored
View
11 lib/twostroke/ast/expression_statement.rb
@@ -0,0 +1,11 @@
+module Twostroke::AST
+ class ExpressionStatement < Base
+ attr_accessor :expr
+
+ def walk(&bk)
+ if yield self
+ expr.walk &bk
+ end
+ end
+ end
+end
View
5 lib/twostroke/compiler/tsasm.rb
@@ -679,6 +679,11 @@ def BracketedExpression(node)
compile node.value
end
+ def ExpressionStatement(node)
+ compile node.expr
+ output :pop
+ end
+
def Void(node)
compile node.value
output :pop
View
8 lib/twostroke/parser.rb
@@ -72,7 +72,7 @@ def statement(consume_semicolon = true)
when :SEMICOLON; nil
when :LINE_TERMINATOR; nil
when :BAREWORD; label
- else; expression
+ else; expression_statement
end
if consume_semicolon
if try_peek_token and peek_token.type == :SEMICOLON
@@ -91,10 +91,14 @@ def label
return AST::Label.new name: name, line: token.line, statement: statement(false)
else
load_state state
- expression
+ expression_statement
end
end
+ def expression_statement
+ AST::ExpressionStatement.new expr: expression
+ end
+
def expression
multi_expression
end
View
4 repl.rb
@@ -105,6 +105,10 @@ def repl_inspect(obj)
end
bytecode[:"repl_#{sect}_main"][-2] = [:ret] # hacky way to make main return the last evaluated value
+ if bytecode[:"repl_#{sect}_main"][-3] == [:pop]
+ # hack around the auto pop of expression statements:
+ bytecode[:"repl_#{sect}_main"][-3] = [:ret]
+ end
obj = nil
exception = catch(:exception) { obj = vm.execute(:"repl_#{sect}_main", vm.global_scope) || Twostroke::Runtime::Types::Undefined.new; nil }
if exception

0 comments on commit 7eff0be

Please sign in to comment.
Something went wrong with that request. Please try again.