Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanup...

  • Loading branch information...
commit 9ed34ac7a0991cd44e25e2bfdb28d8fe5c20ab6a 1 parent 74fbe12
@parse authored
Showing with 21 additions and 20 deletions.
  1. +19 −19 lib/Parser.rb
  2. +2 −1  nxx1.txt
View
38 lib/Parser.rb
@@ -6,7 +6,6 @@
require File.join(File.dirname(__FILE__), 'Node')
class Parser
- attr_accessor :ast
def initialize(sourceText, verbose = false)
@sourceText = sourceText
@@ -20,6 +19,7 @@ def initialize(sourceText, verbose = false)
parse()
end
+ # Return AST Node Tree
def getAST()
return @ast
end
@@ -77,7 +77,6 @@ def foundOneOf(argTokenTypes)
# argTokenTypes should be a list of argTokenType
for argTokenType in argTokenTypes
- #print "foundOneOf", argTokenType, token.type
if @token.type == argTokenType
return true
end
@@ -88,7 +87,6 @@ def foundOneOf(argTokenTypes)
# Found
def found(argTokenType)
- #puts @token.type + " == " + argTokenType
if @token.type == argTokenType
return true
end
@@ -111,8 +109,9 @@ def consume(argTokenType)
# Program
def program()
- push("program")
# program = statement {statement} EOF.
+
+ push("program")
node = Node.new()
statement(node)
@@ -128,12 +127,12 @@ def program()
# Statement
def statement(node)
- push("statement")
-
# statement = printStatement | assignmentStatement .
# assignmentStatement = variable "=" expression ";".
# printStatement = "print" expression ";".
+ push("statement")
+
if found("print")
printStatement(node)
else
@@ -145,13 +144,13 @@ def statement(node)
# Expression
def expression(node)
- push("expression")
-
# expression = stringExpression | numberExpression.
# "||" is the concatenation operator, as in PL/I
# stringExpression = (stringLiteral | variable) {"||" stringExpression}.
# numberExpression = (numberLiteral | variable) { numberOperator numberExpression}.
# numberOperator = "+" | "-" | "/" | "*" .
+
+ push("expression")
if found(WHITESPACE) or found(COMMENT)
getToken()
@@ -194,8 +193,10 @@ def expression(node)
# AssignmentStatement
def assignmentStatement(node)
- push("assignmentStatement")
#assignmentStatement = variable "=" expression ";".
+
+ push("assignmentStatement")
+
identifierNode = Node.new(@token)
consume(IDENTIFIER)
@@ -212,8 +213,10 @@ def assignmentStatement(node)
# printStatement
def printStatement(node)
- push("printStatement")
# printStatement = "print" expression ";".
+
+ push("printStatement")
+
statementNode = Node.new(@token)
consume("print")
@@ -227,10 +230,11 @@ def printStatement(node)
# stringExpression
def stringExpression(node)
- push("stringExpression")
- # /* "||" is the concatenation operator, as in PL/I */
+ # "||" is the concatenation operator, as in PL/I
#stringExpression = (stringLiteral | variable) {"||" stringExpression}.
+ push("stringExpression")
+
if found(STRING)
node.add(@token)
getToken()
@@ -253,9 +257,11 @@ def stringExpression(node)
# numberExpression
def numberExpression(node)
- push("numberExpression")
# numberExpression = (numberLiteral | variable) { numberOperator numberExpression}.
# numberOperator = "+" | "-" | "/" | "*" .
+
+ push("numberExpression")
+
if found(NUMBER)
numberLiteral(node)
else
@@ -271,12 +277,6 @@ def numberExpression(node)
pop("numberExpression")
end
- # commentLiteral
- def commentLiteral(node)
- node.add(@token)
- getToken()
- end
-
# stringLiteral
def stringLiteral(node)
node.add(@token)
View
3  nxx1.txt
@@ -11,9 +11,10 @@ nxx is a simple programming language that provides:
comments may be enclosed in slash+asterisk .. asterisk+slash
*/
alpha = 16 ;
-beta = 2 ;
+beta = 2 ;
resultName = "delta" ;
delta = alpha / beta ;
print "Value of " || resultName || " is: " ;
+print resultName ;
print delta ;
print "\n" ;
Please sign in to comment.
Something went wrong with that request. Please try again.