Skip to content

Commit

Permalink
Added tuple,list,dict types.
Browse files Browse the repository at this point in the history
  • Loading branch information
vic committed Jan 4, 2011
1 parent c019846 commit 2b77e5f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
35 changes: 22 additions & 13 deletions lib/typhon/ast/nodes/literal.rb
Expand Up @@ -3,55 +3,64 @@ module AST
class ConstNode < Node
def bytecode(g)
pos(g)

if (@value.respond_to?(:to_py))
g.push_literal(@value.to_py)
else
g.push_literal(@value)
end
end
end

class TupleNode < Node
def bytecode(g)
pos(g)

@nodes.each do |node|

g.push_rubinius
g.find_const :Tuple
g.push_literal @nodes.size
g.send :new, 1

@nodes.each_with_index do |node, index|
g.dup
g.push_literal index
node.bytecode(g)
g.send :put, 2
g.pop
end
g.make_array(@nodes.size)
# TODO: This needs to actually make a frozen list of some sort. Tuples are immutable.
end
end
end

class ListNode < TupleNode
def bytecode(g)
pos(g)

@nodes.each do |node|
node.bytecode(g)
end
g.make_array(@nodes.size)
end
end

class DictNode < Node
def bytecode(g)
pos(g)

g.push_cpath_top
g.find_const :Hash
g.push @items.size
g.send :new_from_literal, 1

@items.each do |node|
g.dup
key, value = node
key.bytecode(g)
value.bytecode(g)
g.send(:py_set, 2)
g.send(:[]=, 2)
g.pop
end
# ...
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/typhon/compiler/generator.rb
@@ -0,0 +1,4 @@
module Typhon
module Compiler
end
end
15 changes: 14 additions & 1 deletion lib/typhon/compiler/stages.rb
Expand Up @@ -3,6 +3,19 @@
require 'typhon/ast'

module Typhon

class Generator < Rubinius::Generator
def push_typhon_env
push_cpath_top
find_const :Typhon
find_const :Environment
end

def py_send(name, arity)
send name, arity
end
end

class Stage


Expand All @@ -23,7 +36,7 @@ def run
root = @root.new @input
root.file = @compiler.parser.filename

@output = Rubinius::Generator.new
@output = Typhon::Generator.new
root.variable_scope = @variable_scope
root.bytecode @output

Expand Down
3 changes: 3 additions & 0 deletions lib/typhon/environment.rb
Expand Up @@ -8,6 +8,9 @@
Typhon::Environment.set_python_module(Typhon::Environment::BuiltInModule) do
require 'typhon/environment/numbers'
require 'typhon/environment/string'
require 'typhon/environment/tuple'
require 'typhon/environment/list'
require 'typhon/environment/dict'
require 'typhon/environment/singletons'
require 'typhon/environment/exceptions'
end
21 changes: 21 additions & 0 deletions lib/typhon/environment/dict.rb
@@ -0,0 +1,21 @@
module Typhon
module Environment
class Dict
include PythonObjectMixin

def initialize(hash)
@hash = hash
py_init(DictType)
end
end

python_class_c :DictType, [ObjectBase], 'dict', 'dict' do
end
end
end

class Hash
def to_py
Typhon::Environment::Dict.new(self)
end
end
21 changes: 21 additions & 0 deletions lib/typhon/environment/list.rb
@@ -0,0 +1,21 @@
module Typhon
module Environment
class List
include PythonObjectMixin

def initialize(rb_list)
@list = rb_list
py_init(ListType)
end
end

python_class_c :ListType, [ObjectBase], 'list', 'list' do
end
end
end

class Array
def to_py
Typhon::Environment::List.new(self)
end
end
23 changes: 23 additions & 0 deletions lib/typhon/environment/tuple.rb
@@ -0,0 +1,23 @@
module Typhon
module Environment
class Tuple
include PythonObjectMixin

def initialize(rbx_tuple)
@tuple = rbx_tuple
py_init(TupleType)
end
end

python_class_c :TupleType, [ObjectBase], 'tuple', 'tuple' do

end
end
end

class Rubinius::Tuple
def to_py
Typhon::Environment::Tuple.new(self)
end
end

0 comments on commit 2b77e5f

Please sign in to comment.