Permalink
Browse files

Some refactoring, dont generate token for comments anymore. Started c…

…ompiling
  • Loading branch information...
1 parent f69421c commit 65d1c1b46d04e5e2de25a7d79003cc9385944e5d @parse committed Feb 28, 2012
Showing with 19 additions and 14 deletions.
  1. +3 −1 ParserDriver.rb
  2. +7 −3 lib/{Node.rb → AST.rb}
  3. +1 −1 lib/Lexer.rb
  4. +8 −9 lib/Parser.rb
View
@@ -17,4 +17,6 @@ def get_file_as_string(filename)
puts "~"*40
ast = parser.getAST()
-puts ast.to_s
+puts ast.to_s
+
+puts ast.compile()
View
@@ -1,4 +1,4 @@
-class Node
+class AST
attr_accessor :level, :to_s
def initialize(token=nil)
@@ -9,15 +9,19 @@ def initialize(token=nil)
def add(token)
# Make a node out of a token and add it to self.children
- addNode( Node.new(token) )
+ addNode( AST.new(token) )
end
def addNode(node)
# Add a node to self.children
node.level = @level + 1
@children << node
end
-
+
+ def compile()
+
+ end
+
def to_s()
s = " " * @level
View
@@ -76,7 +76,7 @@ def get()
getChar() # read past the first character of a 2-character token
getChar() # read past the second character of a 2-character token
- return token
+ #return token # Don't return comment tokens
end
end
View
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'Lexer')
require File.join(File.dirname(__FILE__), 'nxxSymbols')
-require File.join(File.dirname(__FILE__), 'Node')
+require File.join(File.dirname(__FILE__), 'AST')
# A recursive descent parser for nxx
class Parser
@@ -106,7 +106,7 @@ def program()
# program = statement {statement} EOF.
push("program")
- node = Node.new()
+ node = AST.new()
statement(node)
while not found(EOF)
@@ -153,8 +153,8 @@ def expression(node)
stringLiteral(node)
while found("||")
- getToken()
- stringExpression(node)
+ getToken()
+ stringExpression(node)
end
elsif found(NUMBER)
@@ -193,10 +193,10 @@ def assignmentStatement(node)
push("assignmentStatement")
- identifierNode = Node.new(@token)
+ identifierNode = AST.new(@token)
consume(IDENTIFIER)
- operatorNode = Node.new(@token)
+ operatorNode = AST.new(@token)
consume("=")
node.addNode(operatorNode)
@@ -213,7 +213,7 @@ def printStatement(node)
push("printStatement")
- statementNode = Node.new(@token)
+ statementNode = AST.new(@token)
consume("print")
node.addNode(statementNode)
@@ -229,9 +229,8 @@ def stringExpression(node)
# "||" is the concatenation operator, as in PL/I
#stringExpression = (stringLiteral | variable) {"||" stringExpression}.
- push("stringExpression")
-
if found(STRING)
+
node.add(@token)
getToken()

0 comments on commit 65d1c1b

Please sign in to comment.