Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented 1 + 1

  • Loading branch information...
commit 0dfa1cfe560ecb3d104a55446600dc6cc112d2a6 0 parents
tomhuda authored Jose and Yehuda committed
17 .gitignore
... ... @@ -0,0 +1,17 @@
  1 +*.gem
  2 +*.rbc
  3 +.bundle
  4 +.config
  5 +.yardoc
  6 +Gemfile.lock
  7 +InstalledFiles
  8 +_yardoc
  9 +coverage
  10 +doc/
  11 +lib/bundler/man
  12 +pkg
  13 +rdoc
  14 +spec/reports
  15 +test/tmp
  16 +test/version_tmp
  17 +tmp
4 Gemfile
... ... @@ -0,0 +1,4 @@
  1 +source 'http://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in thrasos.gemspec
  4 +gemspec
2  Rakefile
... ... @@ -0,0 +1,2 @@
  1 +#!/usr/bin/env rake
  2 +require "bundler/gem_tasks"
62 lib/thrasos.rb
... ... @@ -0,0 +1,62 @@
  1 +require "thrasos/version"
  2 +require "rkelly"
  3 +
  4 +module Thrasos
  5 + class Compiler < RKelly::Visitors::Visitor
  6 + attr_accessor :generator
  7 + alias g generator
  8 +
  9 + def initialize
  10 + @generator = Rubinius::Generator.new
  11 + end
  12 +
  13 + def compile(ast)
  14 + accept ast
  15 +
  16 + rbx_compiler = Rubinius::Compiler.new :encoded_bytecode, :compiled_method
  17 + rbx_compiler.encoder.input generator
  18 + rbx_compiler.run
  19 + end
  20 +
  21 + def visit_SourceElementsNode(o)
  22 + o.value.each { |x| x.accept(self) }
  23 + end
  24 +
  25 + def visit_ExpressionStatementNode(o)
  26 + o.value.accept(self)
  27 + end
  28 +
  29 + def visit_AddNode(o)
  30 + o.left.accept(self)
  31 + o.value.accept(self)
  32 + g.meta_send_op_plus g.find_literal(:+)
  33 + g.ret
  34 + end
  35 +
  36 + def visit_NumberNode(o)
  37 + g.push_int o.value
  38 + end
  39 + end
  40 +
  41 + def self.eval(string)
  42 + parser = RKelly::Parser.new
  43 + ast = parser.parse(string)
  44 + cm = Compiler.new.compile(ast)
  45 + b = binding
  46 +
  47 + cm.scope = b.static_scope
  48 + cm.name = :script
  49 +
  50 + script = Rubinius::CompiledMethod::Script.new(cm, "(eval)", true)
  51 + script.eval_source = string
  52 +
  53 + cm.scope.script = script
  54 +
  55 + be = Rubinius::BlockEnvironment.new
  56 + be.under_context b.variables, cm
  57 +
  58 + be.from_eval!
  59 + be.set_eval_binding b
  60 + be.call_on_instance(b.self)
  61 + end
  62 +end
3  lib/thrasos/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module Thrasos
  2 + VERSION = "0.0.1"
  3 +end
7 spec/arithmetic_spec.rb
... ... @@ -0,0 +1,7 @@
  1 +require "spec_helper"
  2 +
  3 +describe "Arithmetic" do
  4 + it "can add integers" do
  5 + Thrasos.eval("1+1").should == 2
  6 + end
  7 +end
2  spec/spec_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +require "bundler/setup"
  2 +require "thrasos"
20 thrasos.gemspec
... ... @@ -0,0 +1,20 @@
  1 +# -*- encoding: utf-8 -*-
  2 +require File.expand_path('../lib/thrasos/version', __FILE__)
  3 +
  4 +Gem::Specification.new do |gem|
  5 + gem.authors = ["José Valim", "Yehuda Katz"]
  6 + gem.email = ["jose.valim@gmail.com", "wycats@gmail.com"]
  7 + gem.description = %q{TODO: Write a gem description}
  8 + gem.summary = %q{TODO: Write a gem summary}
  9 + gem.homepage = ""
  10 +
  11 + gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
  12 + gem.files = `git ls-files`.split("\n")
  13 + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
  14 + gem.name = "thrasos"
  15 + gem.require_paths = ["lib"]
  16 + gem.version = Thrasos::VERSION
  17 +
  18 + gem.add_dependency "rkelly"
  19 + gem.add_development_dependency "rspec"
  20 +end

0 comments on commit 0dfa1cf

Please sign in to comment.
Something went wrong with that request. Please try again.