Skip to content

Commit

Permalink
started to implement simple object system (tuples).
Browse files Browse the repository at this point in the history
more to come.
  • Loading branch information
bakkdoor committed Sep 3, 2009
1 parent ff66dea commit 9e68e89
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions examples/tuples.stackd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
in: examples.tuples ;

tuple: foo bar baz ;

$! pp ;
29 changes: 26 additions & 3 deletions runtime/primitives/syntax.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module Primitives
module Syntax
def get_current_module(scope)
mod_name = scope["*module_name*"]
if mod_name
scope[mod_name]
else
nil
end
end

def init_primitive_syntax
syntax('require:') do |scope, atoms|
filename = atoms.first.eval(scope)
Expand All @@ -11,15 +20,29 @@ def init_primitive_syntax
first = atoms.first
case first
when Stackd::Identifier
mod_name = scope["*module_name*"]
if mod_name
scope[mod_name].define_word(first.text_value, atoms[1..-1])
mod = get_current_module(scope)
if mod
mod.define_word(first.text_value, atoms[1..-1])
else
scope.define_word(first.text_value, atoms[1..-1])
end
end
end

syntax('tuple:') do |scope, atoms|
tuplename = atoms.first.text_value
slots = atoms.rest.map{|a| a.text_value}
case atoms.first
when Stackd::Identifier
mod = get_current_module(scope)
if mod
mod.define_tuple(tuplename, slots)
else
scope.define_tuple(tuplename, slots)
end
end
end

syntax('in:') do |scope, atoms|
module_name = atoms.first.text_value
scope["*modules*"] << module_name
Expand Down
9 changes: 9 additions & 0 deletions runtime/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class Scope
def initialize(parent = nil)
@parent = parent || {}
@symbols = {}
@tuples = {}
end

def tuples
@tuples
end

def [](name)
Expand All @@ -19,6 +24,10 @@ def define_word(name, *body, &block)
def syntax(name, &block)
self[name] = Syntax.new(self, &block)
end

def define_tuple(name, slots)
self.tuples[name] = Tuple.new(name, slots)
end
end

class TopLevel < Scope
Expand Down
3 changes: 2 additions & 1 deletion runtime/stackd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"primitives/words",
"nodes",
"scope",
"word"].each do |path|
"word",
"tuple"].each do |path|
require File.dirname(__FILE__) + '/' + path
end

Expand Down
15 changes: 15 additions & 0 deletions runtime/tuple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Tuple
def initialize(name, slots)
@name = name
@slots = slots || []
# self.class.instance_eval{ attr_accessor *slots }
end

def to_s
"#<Tuple:#{@name} [#{@slots.join(',')}]>"
end

def inspect
to_s
end
end

0 comments on commit 9e68e89

Please sign in to comment.