Skip to content
Browse files

Added tuple,list,dict types.

  • Loading branch information...
1 parent c019846 commit 2b77e5fae0d0801a26ffc227c5ac7a384bf705c4 @vic committed Jan 4, 2011
View
35 lib/typhon/ast/nodes/literal.rb
@@ -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
View
4 lib/typhon/compiler/generator.rb
@@ -0,0 +1,4 @@
+module Typhon
+ module Compiler
+ end
+end
View
15 lib/typhon/compiler/stages.rb
@@ -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
@@ -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
View
3 lib/typhon/environment.rb
@@ -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
View
21 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
View
21 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
View
23 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.
Something went wrong with that request. Please try again.