salaam boots and classes are shown

This commit is contained in:
Torsten Ruger
2015-07-29 20:50:39 +03:00
parent 264a52c184
commit 9383f0d75e
5 changed files with 32 additions and 7 deletions

View File

@ -0,0 +1,37 @@
# A simple event registering/triggering module to mix into classes.
# Events are stored in the `@events` ivar.
module Eventable
# Register a handler for the given event name.
# The event name is the method name called on the handler object
#
# obj.on(:foo , some_object_that_implements foo( whateverargs)
#
# @param [String, Symbol] name event name
# @param [Object] object handling the event, ie implement the function name
# @return handler
def register_event(name, handler)
event_table[name] << handler
handler
end
def unregister_event(name, handler)
event_table[name].delete handler
end
def event_table
return @event_table if @event_table
@event_table = Hash.new { |hash, key| hash[key] = [] }
end
# Trigger the given event name and passes all args to each handler
# for this event.
#
# obj.trigger(:foo)
# obj.trigger(:foo, 1, 2, 3)
#
# @param [String, Symbol] name event name to trigger
def trigger(name, *args)
event_table[name].each { |handler| handler.send( name.to_sym , *args) }
end
end

View File

@ -0,0 +1,166 @@
require_relative "eventable"
class Interpreter
# fire events for changed pc and register contents
include Eventable
# current instruction or pc
attr_reader :instruction
# an (arm style) link register. store the return address to return to
attr_reader :link
# current executing block. since this is not a hardware simulator this is luxury
attr_reader :block
# the registers, 12
attr_reader :registers
# collect the output
attr_reader :stdout
attr_reader :state
def initialize
@state = "runnnig"
@stdout = ""
@registers = {}
(0...16).each do |r|
set_register "r#{r}".to_sym , "r#{r}:unknown"
end
end
def start bl
set_block bl
end
def set_block bl
return if @block == bl
raise "Error, nil block" unless bl
old = @block
@block = bl
trigger(:block_changed , old , bl)
set_instruction bl.codes.first
end
def set_instruction i
@state = "exited" unless i
return if @instruction == i
old = @instruction
@instruction = i
trigger(:instruction_changed, old , i)
end
def get_register( reg )
reg = reg.symbol if reg.is_a? Register::RegisterReference
raise "Not a register #{reg}" unless Register::RegisterReference.look_like_reg(reg)
@registers[reg]
end
def set_register reg , val
old = get_register( reg ) # also ensures format
return if old === val
reg = reg.symbol if reg.is_a? Register::RegisterReference
@registers[reg] = val
trigger(:register_changed, reg , old , val)
end
def tick
return unless @instruction
name = @instruction.class.name.split("::").last
fetch = send "execute_#{name}"
return unless fetch
fetch_next_intruction
end
def fetch_next_intruction
if(@instruction != @block.codes.last)
set_instruction @block.codes[ @block.codes.index(@instruction) + 1]
else
next_b = @block.method.source.blocks.index(@block) + 1
set_block @block.method.source.blocks[next_b]
end
end
def object_for reg
id = get_register(reg)
Virtual.machine.objects[id]
end
# Instruction interpretation starts here
def execute_Branch
target = @instruction.block
set_block target
false
end
def execute_LoadConstant
to = @instruction.register
value = @instruction.constant.object_id
set_register( to , value )
true
end
def execute_GetSlot
object = object_for( @instruction.array )
value = object.internal_object_get( @instruction.index )
value = value.object_id unless value.is_a? Integer
set_register( @instruction.register , value )
true
end
def execute_SetSlot
value = object_for( @instruction.register )
object = object_for( @instruction.array )
object.internal_object_set( @instruction.index , value )
trigger(:object_changed, @instruction.register )
true
end
def execute_RegisterTransfer
value = get_register @instruction.from
set_register @instruction.to , value
true
end
def execute_FunctionCall
@link = [@block , @instruction]
next_block = @instruction.method.source.blocks.first
set_block next_block
false
end
def execute_SaveReturn
object = object_for @instruction.register
raise "save return has nothing to save" unless @link
trigger(:object_changed, @instruction.register )
object.internal_object_set @instruction.index , @link
true
end
def execute_Syscall
name = @instruction.name
case name
when :putstring
str = object_for( :r1 ) # should test length, ie r2
raise "NO string for putstring #{str}" unless str.is_a? Symbol
@stdout += str.to_s
when :exit
set_instruction(nil)
return false
else
raise "un-implemented syscall #{name}"
end
true
end
def execute_FunctionReturn
object = object_for( @instruction.register )
#wouldn't need to assign to link, but makes tsting easier
@link = object.internal_object_get( @instruction.index )
@block , @instruction = @link
# we jump back to the call instruction. so it is as if the call never happened and we continue
true
end
end

View File

@ -1,11 +1,12 @@
# By default Volt generates this controller for your Main component
require "salama"
require_relative "interpreter"
module Main
class MainController < Volt::ModelController
def index
# code = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:putstring, [] ,Ast::StringExpression.new("Hello again"))])
# Virtual::Compiler.compile( code , machine.space.get_main )
# machine.run_before "Register::CallImplementation"
# interpreter.start machine.init
init_machine
init_classes
end
def about
@ -13,7 +14,22 @@ module Main
end
private
def init_machine
machine = Virtual.machine.boot
machine.run_before "Register::CallImplementation"
code = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:putstring, [] ,Ast::StringExpression.new("Hello again"))])
Virtual::Compiler.compile( code , machine.space.get_main )
@interpreter = Interpreter.new
@interpreter.start machine.init
end
def init_classes
page._classes!.clear
Virtual.machine.space.classes.each do |name , claz|
next if [:Kernel,:Module,:MetaClass,:BinaryCode].index name
c = Volt::Model.new :name => name
page._classes << c
end
end
# The main template contains a #template binding that shows another
# template. This is the path to that template. It may change based
# on the params._component, params._controller, and params._action values.

View File

@ -2,8 +2,15 @@
Home
<:Body>
<div class="classes">
<h4> Classes </h4>
{{page._classes!.each do |clas| }}
<div class="one-class">
{{ clas._name }}
</div>
{{end}}
</div>
ClassView classes: machine.space.classes
<div class="file-view">
"Future Source code view"
</div>