Bug with brackets in parsing resolved.
parent
8dc5802c2b
commit
4cec642234
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
ASTNode::ASTNode() {
|
ASTNode::ASTNode() {
|
||||||
// TODO Auto-generated constructor stub
|
// TODO Auto-generated constructor stub
|
||||||
|
parent = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode::~ASTNode() {
|
ASTNode::~ASTNode() {
|
||||||
|
|
|
@ -7,10 +7,17 @@
|
||||||
|
|
||||||
#ifndef ASTNODE_H_
|
#ifndef ASTNODE_H_
|
||||||
#define ASTNODE_H_
|
#define ASTNODE_H_
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class ASTNode {
|
class ASTNode {
|
||||||
public:
|
public:
|
||||||
ASTNode();
|
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();
|
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
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
|
../ASTNode.cpp \
|
||||||
|
../ASTPrimary.cpp \
|
||||||
../Lexer.cpp \
|
../Lexer.cpp \
|
||||||
../Object.cpp \
|
../Object.cpp \
|
||||||
../Parser.cpp \
|
../Parser.cpp \
|
||||||
|
@ -11,6 +13,8 @@ CPP_SRCS += \
|
||||||
../main.cpp
|
../main.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
|
./ASTNode.o \
|
||||||
|
./ASTPrimary.o \
|
||||||
./Lexer.o \
|
./Lexer.o \
|
||||||
./Object.o \
|
./Object.o \
|
||||||
./Parser.o \
|
./Parser.o \
|
||||||
|
@ -18,6 +22,8 @@ OBJS += \
|
||||||
./main.o
|
./main.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
|
./ASTNode.d \
|
||||||
|
./ASTPrimary.d \
|
||||||
./Lexer.d \
|
./Lexer.d \
|
||||||
./Object.d \
|
./Object.d \
|
||||||
./Parser.d \
|
./Parser.d \
|
||||||
|
|
|
@ -194,28 +194,34 @@ void Parser::prim_expr()
|
||||||
if(current_token.get_type() == t_integer)
|
if(current_token.get_type() == t_integer)
|
||||||
{
|
{
|
||||||
report("Number: " + tok_value + "\n");
|
report("Number: " + tok_value + "\n");
|
||||||
|
read_next();
|
||||||
}
|
}
|
||||||
else if(current_token.get_type() == t_symbol)
|
else if(current_token.get_type() == t_symbol)
|
||||||
{
|
{
|
||||||
report("Variable: " + tok_value + "\n");
|
report("Variable: " + tok_value + "\n");
|
||||||
|
read_next();
|
||||||
}
|
}
|
||||||
else if(current_token.get_type() == t_literal)
|
else if(current_token.get_type() == t_literal)
|
||||||
{
|
{
|
||||||
report("Character literal: " + tok_value + "\n");
|
report("Character literal: " + tok_value + "\n");
|
||||||
|
read_next();
|
||||||
}
|
}
|
||||||
else if(accept("("))
|
else if(accept("("))
|
||||||
{
|
{
|
||||||
|
report("( ");
|
||||||
expr();
|
expr();
|
||||||
|
report(" ) ");
|
||||||
expect(")");
|
expect(")");
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
error("ERROR: unexpected primary expression" + tok_value + "\n");
|
error("ERROR: unexpected primary expression" + tok_value + "\n");
|
||||||
|
|
||||||
}
|
|
||||||
read_next();
|
read_next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Parser::postfix_expr()
|
void Parser::postfix_expr()
|
||||||
{
|
{
|
||||||
prim_expr();
|
prim_expr();
|
||||||
|
@ -237,24 +243,42 @@ void Parser::postfix_expr()
|
||||||
expr();
|
expr();
|
||||||
report("function argument\n");
|
report("function argument\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(")");
|
expect(")");
|
||||||
}
|
}
|
||||||
report("FUNC_CALL\n");
|
report("FUNC_CALL\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::add_expr()
|
void Parser::mul_expr()
|
||||||
{
|
{
|
||||||
postfix_expr();
|
postfix_expr();
|
||||||
|
while(peek("*") || peek("/"))
|
||||||
|
{
|
||||||
|
if(accept("*"))
|
||||||
|
{
|
||||||
|
postfix_expr();
|
||||||
|
report(" *\n");
|
||||||
|
} else if(accept("/"))
|
||||||
|
{
|
||||||
|
postfix_expr();
|
||||||
|
report(" /\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Parser::add_expr()
|
||||||
|
{
|
||||||
|
mul_expr();
|
||||||
while(peek("+") || peek("-"))
|
while(peek("+") || peek("-"))
|
||||||
{
|
{
|
||||||
if(accept("+"))
|
if(accept("+"))
|
||||||
{
|
{
|
||||||
postfix_expr();
|
mul_expr();
|
||||||
report(" +\n");
|
report(" +\n");
|
||||||
} else if(accept("-"))
|
} else if(accept("-"))
|
||||||
{
|
{
|
||||||
postfix_expr();
|
mul_expr();
|
||||||
report(" -\n");
|
report(" -\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Parser
|
||||||
|
|
||||||
void statement();
|
void statement();
|
||||||
|
|
||||||
|
void mul_expr();
|
||||||
void add_expr();
|
void add_expr();
|
||||||
void prim_expr();
|
void prim_expr();
|
||||||
void postfix_expr();
|
void postfix_expr();
|
||||||
|
|
Loading…
Reference in New Issue