Skip to content

Commit

Permalink
Created folder tt_ruby with a TreeTop's (very initial) version of Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiano committed Dec 20, 2011
1 parent d5d867c commit ca3f292
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tt_ruby/ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'polyglot'
require 'treetop'

Treetop.load 'ruby'

class NumberNode < Treetop::Runtime::SyntaxNode
def eval(context)
text_value.to_i
end
end

class StringNode < Treetop::Runtime::SyntaxNode
def eval(context)
text_value[1..-2]
end
end

class TrueNode < Treetop::Runtime::SyntaxNode
def eval(context)
true
end
end

class FalseNode < Treetop::Runtime::SyntaxNode
def eval(context)
false
end
end

class NilNode < Treetop::Runtime::SyntaxNode
def eval(context)
nil
end
end

class SelfNode < Treetop::Runtime::SyntaxNode
def eval(context)
context.current_self
end
end

class ExpressionsNode < Treetop::Runtime::SyntaxNode
def eval(context)
others.elements.inject(expression.eval(context)) { |memo, e| e.expression.eval(context) }
end
end

class AssignmentNode < Treetop::Runtime::SyntaxNode
def eval(context)
context.locals[identifier.eval(context)] = expression.eval(context)
end
end

class IdentifierNode < Treetop::Runtime::SyntaxNode
def eval(context)
text_value.to_sym
end
end

Context = Struct.new(:current_self, :locals)
@context = Context.new(nil, {})

@parser = RubyParser.new
81 changes: 81 additions & 0 deletions tt_ruby/ruby.treetop
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
grammar Ruby
rule root
expressions
end

rule expressions
expression others:(terminator expression)* <ExpressionsNode>
end

rule expression
literal
/
self
/
assignment
end

rule terminator
newline
/
';'
end

rule newline
"\n"
end

rule literal
number
/
string
/
true
/
false
/
nil
end

rule number
'-'? [1-9] [0-9]* <NumberNode>
end

rule string
string_with_double_quotes
/
string_with_single_quotes
end

rule string_with_double_quotes
'"' (!'"' .)* '"' <StringNode>
end

rule string_with_single_quotes
"'" (!"'" .)* "'" <StringNode>
end

rule true
'true' <TrueNode>
end

rule false
'false' <FalseNode>
end

rule nil
'nil' <NilNode>
end

rule self
'self' <SelfNode>
end

rule assignment
identifier '=' expression <AssignmentNode>
end

rule identifier
[a-z] [a-zA-Z_]* <IdentifierNode>
end
end

0 comments on commit ca3f292

Please sign in to comment.