add the idea of a frame

This commit is contained in:
Torsten Ruger
2014-06-29 19:05:35 +03:00
parent 16ceb2a60d
commit 18faeeb042
5 changed files with 44 additions and 14 deletions

View File

@ -4,7 +4,7 @@ module Ast
class IntegerExpression < Expression
# attr_reader :value
def compile binding
def compile frame
Virtual::IntegerConstant.new value
end
end
@ -13,9 +13,10 @@ module Ast
# attr_reader :name
# compiling a variable resolves it. if it wasn't defined, raise an exception
def compile context
raise "Undefined variable #{name}, defined locals #{context.locals.keys.join('-')}" unless context.locals.has_key?(name)
context.locals[name]
def compile frame
# either a variable or needs to be called.
frame.get(name)
# frame.send name
end
end

20
lib/virtual/frame.rb Normal file
View File

@ -0,0 +1,20 @@
module Virtual
# A frame, or activation frame, represents a function call during calling. So not the static definition of the function
# but the dynamic invokation of it.
#
# In a minimal c world this would be just the return address, but with exceptions and continuations things get more
# complicated. How much more we shall see
#
# The current list comprises
# - next normal instruction
# - next exception instruction
# - self (me)
# - argument mappings
# - local variable mapping
class Frame
def initialize
end
attr_reader :next_normal, :next_exception, :me, :argument_names
end
end

View File

@ -7,8 +7,16 @@ module Virtual
# It is actully the point of the virtual machine layer to express oo functionality in the set of instructions, thus
# defining a minimal set of instructions needed to implement oo.
# This is partly because jumping over this layer and doing in straight in assember was too bi a step
# This is partly because jumping over this layer and doing in straight in assember was too big a step
class Instruction
end
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
# an implementation (in a programm it would be a function). But in a virtual machine, not only do we need this instruction,
# it is indeed the first instruction as just this instruction is the smallest possible programm for the machine.
# As such it is the next instruction for any first instruction that we generate.
class Halt < Instruction
end
end

View File

@ -58,6 +58,7 @@ end
require_relative "list"
require_relative "instruction"
require_relative "frame"
require_relative "value"
require_relative "mystery"
require_relative "object"