Permalink
Browse files

Added many more operators and primary expressions.

  • Loading branch information...
1 parent 2d9ffb8 commit e46c64de68961fdd14e6a580c45e59da676204c4 @trishume committed Apr 1, 2012
Showing with 48 additions and 4 deletions.
  1. +10 −1 include/TuringParser/TuringParser.h
  2. +1 −1 src/Main.cpp
  3. +2 −1 src/Parser.cpp
  4. +35 −1 src/TuringParser.cpp
@@ -10,7 +10,7 @@ namespace TuringParser {
namespace Precedence {
//! Precedence levels from weakest to strongest binding
enum Levels {
- ASSIGNMENT = 1,
+ ASSIGN = 1,
IMPLIES,
OR,
AND,
@@ -45,6 +45,15 @@ namespace TuringParser {
private:
ASTNode::Token Type;
};
+ // works for string AND character literals
+ class StringLiteralParselet : public PrefixOp {
+ public:
+ explicit StringLiteralParselet(ASTNode::Token type) :
+ Type(type) {}
+ virtual ASTNode *parse(Parser *parser, Token token);
+ private:
+ ASTNode::Token Type;
+ };
}
}
View
@@ -40,7 +40,7 @@ void testLexer(std::string fileName) {
int main(int argc, char** argv)
{
- SourceFile *f = new SourceFile("6*7 + 4 - variable div lol**6.0");
+ SourceFile *f = new SourceFile("6*7 + 4 - variable div lol**6.0 and true or false");
Lexer lex(f);
TuringFileParser parser(lex);
View
@@ -76,7 +76,8 @@ namespace TuringParser {
if (PrefixOps.find(token.Type) == PrefixOps.end()) {
std::ostringstream os;
- os << "Could not parse \"" << token.String << "\"";
+ os << "Unexpected" << Token::getHumanTokenName(token.Type) <<
+ ": \"" << token.String << "\"";
ParseError err(curTok().Begin,os.str());
err.setEnd(curTok().getEnd());
throw err;
View
@@ -5,15 +5,44 @@ namespace TuringParser {
// -----------------------------------------------------------------
// register all the prefix parselets: Unary operators and primaries
// -----------------------------------------------------------------
+ // primary expressions
registerPrefixOp(Token::IDENTIFIER, new Parselet::PrimaryParselet(ASTNode::VAR_REFERENCE));
registerPrefixOp(Token::INTLITERAL, new Parselet::PrimaryParselet(ASTNode::INT_LITERAL));
registerPrefixOp(Token::REALLITERAL, new Parselet::PrimaryParselet(ASTNode::REAL_LITERAL));
+ registerPrefixOp(Token::TRUE, new Parselet::PrimaryParselet(ASTNode::BOOL_LITERAL));
+ registerPrefixOp(Token::FALSE, new Parselet::PrimaryParselet(ASTNode::BOOL_LITERAL));
+ registerPrefixOp(Token::STRINGLITERAL,
+ new Parselet::StringLiteralParselet(ASTNode::STRING_LITERAL));
+ registerPrefixOp(Token::CHARLITERAL,
+ new Parselet::StringLiteralParselet(ASTNode::CHAR_LITERAL));
+ // unary operators
registerPrefixOp(Token::OP_MINUS, new Parselet::UnaryOp(ASTNode::UNARY_OP));
+ registerPrefixOp(Token::OP_NOT, new Parselet::UnaryOp(ASTNode::UNARY_OP));
+ registerPrefixOp(Token::OP_DEREF, new Parselet::UnaryOp(ASTNode::UNARY_OP));
// -----------------------------------------------------------------
// register the infix parselets: Binary and postfix operators, calls
// -----------------------------------------------------------------
Parselet::InfixOp *op;
- // additive operators
+ op = new Parselet::BinaryOp(ASTNode::ASSIGN_OP, Precedence::ASSIGN,false);
+ registerInfixOp(Token::OP_ASSIGN, op);
+ registerInfixOp(Token::OP_ASSIGN_DIVIDE, op);
+ registerInfixOp(Token::OP_ASSIGN_MINUS, op);
+ registerInfixOp(Token::OP_ASSIGN_MULT, op);
+ registerInfixOp(Token::OP_ASSIGN_PLUS, op);
+ op = new Parselet::BinaryOp(ASTNode::BIN_OP, Precedence::OR,false);
+ registerInfixOp(Token::OP_OR, op);
+ op = new Parselet::BinaryOp(ASTNode::BIN_OP, Precedence::AND,false);
+ registerInfixOp(Token::OP_AND, op);
+ op = new Parselet::BinaryOp(ASTNode::BIN_OP, Precedence::COMPARE,false);
+ registerInfixOp(Token::OP_GT, op);
+ registerInfixOp(Token::OP_GE, op);
+ registerInfixOp(Token::OP_LT, op);
+ registerInfixOp(Token::OP_LE, op);
+ registerInfixOp(Token::OP_IN, op);
+ // TODO not in operator
+ op = new Parselet::BinaryOp(ASTNode::EQUALITY_OP, Precedence::COMPARE,false);
+ registerInfixOp(Token::OP_EQ, op);
+ registerInfixOp(Token::OP_NOT_EQ, op);
op = new Parselet::BinaryOp(ASTNode::BIN_OP, Precedence::ADD,false);
registerInfixOp(Token::OP_MINUS, op);
registerInfixOp(Token::OP_PLUS, op);
@@ -53,5 +82,10 @@ namespace TuringParser {
ASTNode *PrimaryParselet::parse(Parser *parser, Token token) {
return new ASTNode(Type,token.Begin,token.String);
}
+ ASTNode *StringLiteralParselet::parse(Parser *parser, Token token) {
+ // strip the opening and closing quotes
+ return new ASTNode(Type,token.Begin,
+ token.String.substr(1,token.String.size()-2));
+ }
}
}

0 comments on commit e46c64d

Please sign in to comment.