zwischenstopp
This commit is contained in:
parent
7045a4b256
commit
f74999af57
@ -28,11 +28,9 @@ module Ast
|
|||||||
class NameExpression < Expression
|
class NameExpression < Expression
|
||||||
# attr_reader :name
|
# attr_reader :name
|
||||||
|
|
||||||
# compiling a variable resolves it. if it wasn't defined, raise an exception
|
# compiling a variable resolves it. If it's not defined look call it as a menthod (which may raise NoMethodFound)
|
||||||
def compile frame
|
def compile frame
|
||||||
# either a variable or needs to be called.
|
|
||||||
frame.get(name)
|
frame.get(name)
|
||||||
# frame.send name
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -10,11 +10,17 @@ module Virtual
|
|||||||
# - next exception instruction
|
# - next exception instruction
|
||||||
# - self (me)
|
# - self (me)
|
||||||
# - argument mappings
|
# - argument mappings
|
||||||
# - local variable mapping
|
# - local variable mapping, together with last called binding
|
||||||
class Frame
|
class Frame
|
||||||
def initialize
|
def initialize normal , exceptional , me
|
||||||
|
@next_normal = normal
|
||||||
|
@next_exception = exceptional
|
||||||
|
@me = me
|
||||||
|
# a binding represents the local variables at a point in the program.
|
||||||
|
# The amount of local variables is assumed to be relatively small, and so the
|
||||||
|
# storage is a linked list. Has the same api as a ha
|
||||||
|
@binding = List.new
|
||||||
end
|
end
|
||||||
attr_reader :next_normal, :next_exception, :me, :argument_names
|
attr_reader :next_normal, :next_exception, :me, :binding
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -37,12 +37,10 @@ module Virtual
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
# a binding represents the local variables at a point in the program.
|
the_end = HaltInstruction.new
|
||||||
# The amount of local variables is assumed to be relatively small, and so the
|
@frame = Frame.new(the_end , the_end , :Object)
|
||||||
# storage is a linked list. Has the same api as a ha
|
|
||||||
@bindings = List.new
|
|
||||||
end
|
end
|
||||||
attr_reader :bindings
|
attr_reader :frame
|
||||||
|
|
||||||
# run the instruction stream given. Instructions are a graph and executing means traversing it.
|
# run the instruction stream given. Instructions are a graph and executing means traversing it.
|
||||||
# If there is no next instruction the machine stops
|
# If there is no next instruction the machine stops
|
||||||
@ -53,6 +51,10 @@ module Virtual
|
|||||||
instruction = next_instruction
|
instruction = next_instruction
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
#return an anonymous new function (the top level) into which code is compiled
|
||||||
|
def anonymous
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -9,10 +9,12 @@ module Virtual
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class TrueValue < Value
|
class Singleton < Value
|
||||||
end
|
end
|
||||||
class FalseValue < Value
|
class TrueValue < Singleton
|
||||||
end
|
end
|
||||||
class NilValue < Value
|
class FalseValue < Singleton
|
||||||
|
end
|
||||||
|
class NilValue < Singleton
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -32,19 +32,6 @@ class TestBasic < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_name_underscode_start
|
|
||||||
@string_input = '_bar '
|
|
||||||
@output = Ast::NameExpression.new('_bar')
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_name_underscode_middle
|
|
||||||
@string_input = 'foo_bar '
|
|
||||||
@parse_output = {:name => 'foo_bar'}
|
|
||||||
@output = Ast::NameExpression.new('foo_bar')
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_instance_variable
|
def test_instance_variable
|
||||||
@string_input = '@foo_bar '
|
@string_input = '@foo_bar '
|
||||||
@parse_output = {:instance_variable=>{:name=>"foo_bar"}}
|
@parse_output = {:instance_variable=>{:name=>"foo_bar"}}
|
||||||
@ -74,13 +61,4 @@ class TestBasic < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string_escapes
|
|
||||||
out = 'hello \nyou'
|
|
||||||
@string_input = '"' + out + '"'
|
|
||||||
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"},
|
|
||||||
{:char=>" "}, {:char=>" "}, {:esc=>"n"}, {:char=>"y"}, {:char=>"o"}, {:char=>"u"}]}
|
|
||||||
@output = Ast::StringExpression.new(out)
|
|
||||||
check
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
@ -12,7 +12,7 @@ module VirtualHelper
|
|||||||
syntax = parser.parse_with_debug(@string_input)
|
syntax = parser.parse_with_debug(@string_input)
|
||||||
parts = Parser::Transform.new.apply(syntax)
|
parts = Parser::Transform.new.apply(syntax)
|
||||||
machine = Virtual::Machine.new
|
machine = Virtual::Machine.new
|
||||||
expressions = parts.compile(machine.bindings)
|
expressions = parts.compile(machine.frame , machine.anonymous)
|
||||||
assert_equal @output , expressions
|
assert_equal @output , expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user