Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Parse 1.

  • Loading branch information...
commit 3a43ec11c9ab861a32d2a2c23d0f0af4045eb909 1 parent 80dd90c
@brixen authored
View
17 bin/poison
@@ -1,4 +1,6 @@
#!/usr/bin/env rbx
+#
+# vim: filetype=ruby
$:.unshift File.expand_path('../../lib', __FILE__)
@@ -7,6 +9,7 @@ require 'poison'
class PoisonScript
def initialize
@evals = []
+ @asts = []
end
def options(argv=ARGV)
@@ -30,6 +33,10 @@ class PoisonScript
end
end
+ options.on "-A", "FILE", "Print the AST for FILE" do |code|
+ @asts << code
+ end
+
options.on "-e", "CODE", "Execute CODE" do |e|
@evals << e
end
@@ -50,6 +57,15 @@ class PoisonScript
Poison::CodeLoader.execute_script @evals.join("\n")
end
+ def asts
+ return if @asts.empty?
+
+ require 'pp'
+
+ compiler = Poison::Compiler.new @asts.join("\n")
+ pp compiler.parse.to_sexp
+ end
+
def script
return unless @script
@@ -67,6 +83,7 @@ class PoisonScript
def main
options
evals
+ asts
script
repl
end
View
18 lib/poison/systems/rbx/compiler/grammar.treetop
@@ -3,13 +3,13 @@
module Poison
grammar Poison
rule poison
- hh s:statements end_of_file
+ hh statements end_of_file <Syntax::Script>
end
rule statements
s1:stmt
- (sep s2:stmt)* sep?
- / ''
+ (sep s2:stmt)* sep? <Syntax::Statements>
+ / '' <Syntax::NilKind>
end
rule stmt
@@ -21,7 +21,7 @@ module Poison
rule sets
e:eqs
( assign s:sets
- / or assign s:sets
+ / o:or assign s:sets
/ and assign s:sets
/ pipe assign s:sets
/ caret assign s:sets
@@ -94,7 +94,7 @@ module Poison
( mminus a:atom
/ pplus a:atom
/ a:atom (pplus / mminus)?)
- (call)*
+ (call)* <Syntax::Expression>
end
rule atom
@@ -160,7 +160,7 @@ module Poison
end
rule value
- immed
+ immed <Syntax::Value>
/ lick
end
@@ -169,7 +169,11 @@ module Poison
/ true
/ false
/ hex
- / int
+ / int {
+ def int
+ Syntax::Integer.from text_value
+ end
+ }
/ real
/ imag
/ str1
View
13 lib/poison/systems/rbx/compiler/node.rb
@@ -1,3 +1,14 @@
module Poison
- class Node < Treetop::Runtime::SyntaxNode; end
+ class Treetop::Runtime::SyntaxNode
+ def elements_to_sexp(sexp)
+ elements.each { |e| e.to_sexp(sexp) unless e.terminal? }
+ end
+
+ def to_sexp(sexp)
+ elements_to_sexp sexp
+ end
+ end
+
+ class Node < Treetop::Runtime::SyntaxNode
+ end
end
View
53 lib/poison/systems/rbx/compiler/syntax.rb
@@ -1,16 +1,69 @@
module Poison
class SyntaxError < Exception
+ def initialize(exc)
+ super exc.message
+ end
end
class Syntax < Node
+ class Script < Node
+ def to_sexp(sexp=[])
+ sexp << :script
+ statements.to_sexp sexp
+ sexp
+ end
+ end
+
+ class Statements < Node
+ def to_sexp(sexp)
+ elements_to_sexp sexp
+ end
+ end
+
+ class Expression < Node
+ def to_sexp(sexp)
+ exp = [:expr]
+ elements_to_sexp exp
+ sexp << exp
+ end
+ end
class Boolean < Node
end
class NilKind < Node
+ def to_sexp(sexp)
+ sexp << [:nil]
+ end
end
class Message < Node
end
+
+ class Value < Node
+ def to_sexp(sexp)
+ exp = [:value]
+ if respond_to? :int
+ int.to_sexp exp
+ end
+ sexp << exp
+ end
+ end
+
+ class Immediate < Node
+ attr_accessor :value
+
+ def self.from(text)
+ node = allocate
+ node.value = text
+ node
+ end
+ end
+
+ class Integer < Immediate
+ def to_sexp(sexp)
+ sexp << [value.to_i, nil, nil]
+ end
+ end
end
end
View
1  lib/poison/systems/rbx/library.rb
@@ -0,0 +1 @@
+require 'poison/systems/rbx/library/code_loader'
View
9 lib/poison/systems/rbx/library/code_loader.rb
@@ -0,0 +1,9 @@
+module Poison
+ class CodeLoader
+ def self.execute_script(source)
+ compiler = Compiler.new source
+ ast = compiler.parse
+ p ast
+ end
+ end
+end
View
2  spec/custom/matchers/parse_as.rb
@@ -6,7 +6,7 @@ def initialize(expected)
end
def matches?(actual)
- @actual = Poison::Compiler.new(actual).parse.to_sexp
+ @actual = Poison::Compiler.new(actual).parse.to_sexp.last
@actual == @expected
end
Please sign in to comment.
Something went wrong with that request. Please try again.