-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created folder tt_ruby with a TreeTop's (very initial) version of Ruby
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |