Bug with brackets in parsing resolved.
parent
8dc5802c2b
commit
4cec642234
|
@ -9,7 +9,7 @@
|
|||
|
||||
ASTNode::ASTNode() {
|
||||
// TODO Auto-generated constructor stub
|
||||
|
||||
parent = 0;
|
||||
}
|
||||
|
||||
ASTNode::~ASTNode() {
|
||||
|
|
|
@ -7,10 +7,17 @@
|
|||
|
||||
#ifndef ASTNODE_H_
|
||||
#define ASTNODE_H_
|
||||
#include <vector>
|
||||
|
||||
class ASTNode {
|
||||
public:
|
||||
ASTNode();
|
||||
ASTNode * parent;
|
||||
std::vector<ASTNode *> children;
|
||||
void add_children(ASTNode *);
|
||||
void remove_most_right_children();
|
||||
void set_parent(ASTNode *);
|
||||
virtual void execute() = 0;
|
||||
virtual ~ASTNode();
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* ASTPrimary.cpp
|
||||
*
|
||||
* Created on: Nov 4, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#include "ASTPrimary.h"
|
||||
/*
|
||||
ASTPrimary::ASTPrimary() {
|
||||
// TODO Auto-generated constructor stub
|
||||
|
||||
}
|
||||
|
||||
ASTPrimary::~ASTPrimary() {
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
*/
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* ASTPrimary.h
|
||||
*
|
||||
* Created on: Nov 4, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#ifndef ASTPRIMARY_H_
|
||||
#define ASTPRIMARY_H_
|
||||
#include "ASTNode.h"
|
||||
/*
|
||||
class ASTPrimary : public ASTNode {
|
||||
public:
|
||||
ASTPrimary();
|
||||
virtual ~ASTPrimary();
|
||||
virtual void execute() {};
|
||||
|
||||
};
|
||||
*/
|
||||
#endif /* ASTPRIMARY_H_ */
|
|
@ -0,0 +1,3 @@
|
|||
ASTNode.d: ../ASTNode.cpp ../ASTNode.h
|
||||
|
||||
../ASTNode.h:
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
ASTPrimary.d: ../ASTPrimary.cpp ../ASTPrimary.h ../ASTNode.h
|
||||
|
||||
../ASTPrimary.h:
|
||||
|
||||
../ASTNode.h:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -4,6 +4,8 @@
|
|||
|
||||
# Add inputs and outputs from these tool invocations to the build variables
|
||||
CPP_SRCS += \
|
||||
../ASTNode.cpp \
|
||||
../ASTPrimary.cpp \
|
||||
../Lexer.cpp \
|
||||
../Object.cpp \
|
||||
../Parser.cpp \
|
||||
|
@ -11,6 +13,8 @@ CPP_SRCS += \
|
|||
../main.cpp
|
||||
|
||||
OBJS += \
|
||||
./ASTNode.o \
|
||||
./ASTPrimary.o \
|
||||
./Lexer.o \
|
||||
./Object.o \
|
||||
./Parser.o \
|
||||
|
@ -18,6 +22,8 @@ OBJS += \
|
|||
./main.o
|
||||
|
||||
CPP_DEPS += \
|
||||
./ASTNode.d \
|
||||
./ASTPrimary.d \
|
||||
./Lexer.d \
|
||||
./Object.d \
|
||||
./Parser.d \
|
||||
|
|
|
@ -194,26 +194,32 @@ void Parser::prim_expr()
|
|||
if(current_token.get_type() == t_integer)
|
||||
{
|
||||
report("Number: " + tok_value + "\n");
|
||||
read_next();
|
||||
}
|
||||
else if(current_token.get_type() == t_symbol)
|
||||
{
|
||||
report("Variable: " + tok_value + "\n");
|
||||
read_next();
|
||||
}
|
||||
else if(current_token.get_type() == t_literal)
|
||||
{
|
||||
report("Character literal: " + tok_value + "\n");
|
||||
read_next();
|
||||
}
|
||||
else if(accept("("))
|
||||
{
|
||||
report("( ");
|
||||
expr();
|
||||
report(" ) ");
|
||||
expect(")");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
error("ERROR: unexpected primary expression" + tok_value + "\n");
|
||||
|
||||
read_next();
|
||||
}
|
||||
read_next();
|
||||
|
||||
}
|
||||
|
||||
void Parser::postfix_expr()
|
||||
|
@ -237,24 +243,42 @@ void Parser::postfix_expr()
|
|||
expr();
|
||||
report("function argument\n");
|
||||
}
|
||||
|
||||
expect(")");
|
||||
}
|
||||
report("FUNC_CALL\n");
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::mul_expr()
|
||||
{
|
||||
postfix_expr();
|
||||
while(peek("*") || peek("/"))
|
||||
{
|
||||
if(accept("*"))
|
||||
{
|
||||
postfix_expr();
|
||||
report(" *\n");
|
||||
} else if(accept("/"))
|
||||
{
|
||||
postfix_expr();
|
||||
report(" /\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::add_expr()
|
||||
{
|
||||
postfix_expr();
|
||||
mul_expr();
|
||||
while(peek("+") || peek("-"))
|
||||
{
|
||||
if(accept("+"))
|
||||
{
|
||||
postfix_expr();
|
||||
mul_expr();
|
||||
report(" +\n");
|
||||
} else if(accept("-"))
|
||||
{
|
||||
postfix_expr();
|
||||
mul_expr();
|
||||
report(" -\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ class Parser
|
|||
|
||||
void statement();
|
||||
|
||||
void mul_expr();
|
||||
void add_expr();
|
||||
void prim_expr();
|
||||
void postfix_expr();
|
||||
|
|
Loading…
Reference in New Issue