started filling in new node types, creating them in parser

git-svn-id: svn://anubis/jtlc/trunk@13 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh 2010-01-13 20:48:37 +00:00
parent 8333a037c9
commit 55b80e8f09
5 changed files with 82 additions and 18 deletions

12
nodes/CStatementNode.cc Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <string>
#include "Node.h"
#include "util/refptr.h"
using namespace std;
void CStatementNode::process(FILE * out)
{
fprintf(out, "%s", m_string.c_str());
fprintf(out, "\n");
}

View File

@ -1,6 +1,6 @@
#include "Node.h"
#include <vector> #include <vector>
#include "Node.h"
#include "util/refptr.h" #include "util/refptr.h"
using namespace std; using namespace std;

View File

@ -2,6 +2,7 @@
#ifndef NODE_H #ifndef NODE_H
#define NODE_H NODE_H #define NODE_H NODE_H
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <string> #include <string>
@ -15,31 +16,52 @@ class Node
void addChildren(refptr<Node> other); void addChildren(refptr<Node> other);
std::vector< refptr<Node> > & getChildren() { return m_children; } std::vector< refptr<Node> > & getChildren() { return m_children; }
virtual uint64_t getInteger() { return 0x0ULL; } virtual double getDouble() { return m_double; }
virtual std::string getString() { return ""; } virtual uint64_t getInteger() { return m_integer; }
virtual std::string getString() { return m_string; }
virtual void process(FILE * out) { } virtual void process(FILE * out) { }
protected: protected:
std::vector< refptr<Node> > m_children; std::vector< refptr<Node> > m_children;
uint64_t m_integer;
std::string m_string;
double m_double;
}; };
class CStatementNode : public Node
{
public:
CStatementNode(const std::string & str) { m_string = str; }
virtual void process(FILE * out);
};
class DoubleNode : public Node
{
public:
DoubleNode(double val) { m_double = val; }
};
class IdentifierNode : public Node
{
public:
IdentifierNode(const std::string & str) { m_string = str; }
};
class IntegerNode : public Node class IntegerNode : public Node
{ {
public: public:
IntegerNode(uint64_t integer) { m_integer = integer; } IntegerNode(uint64_t integer) { m_integer = integer; }
unsigned long getInteger() { return m_integer; } };
protected:
uint64_t m_integer; class ProgramNode : public Node
{
}; };
class StringNode : public Node class StringNode : public Node
{ {
public: public:
StringNode(const std::string & str) { m_string = str; } StringNode(const std::string & str) { m_string = str; }
std::string getString() { return m_string; }
protected:
std::string m_string;
}; };
#endif #endif

View File

@ -4,12 +4,31 @@
%{ %{
#include <stdint.h>
#include <string>
#include "parser.h" #include "parser.h"
#include "parser.tab.hh" #include "parser.tab.hh"
#include <string>
using namespace std; using namespace std;
static string build_string; static string build_string;
static uint64_t parseInt(const char * text, int base)
{
uint64_t val = 0;
for (const char * p = text; *p != '\0'; p++)
{
int digit = -1;
if (*p >= '0' && *p <= '9') digit = *p - '0';
else if (*p >= 'A' && *p <= 'Z') digit = *p - 'A';
else if (*p >= 'a' && *p <= 'z') digit = *p - 'a';
if (digit != -1)
{
val *= base;
val += digit;
}
}
return val;
}
%} %}
%x str %x str
@ -47,17 +66,23 @@ static string build_string;
\( return LPAREN; \( return LPAREN;
\) return RPAREN; \) return RPAREN;
/* literals */ /* numeric literals */
[0-9]+ { [0-9][0-9_]* {
*yylval = new IntegerNode(parseInt(yytext, 10));
return INT_LITERAL; return INT_LITERAL;
} }
0x[0-9A-Fa-f]+ { 0x[0-9A-Fa-f][0-9A-Fa-f_]* {
*yylval = new IntegerNode(parseInt(yytext + 2, 16));
return INT_LITERAL; return INT_LITERAL;
} }
0b[01]+ { 0b[01][01_]* {
*yylval = new IntegerNode(parseInt(yytext + 2, 2));
return INT_LITERAL; return INT_LITERAL;
} }
[0-9]*\.[0-9]+([eE]-?[0-9]+)? { [0-9]*\.[0-9]+([eE]-?[0-9]+)? {
double val;
(void) sscanf(yytext, "%lf", &val);
*yylval = new DoubleNode(val);
return REAL_LITERAL; return REAL_LITERAL;
} }
@ -84,6 +109,7 @@ struct return STRUCT;
/* identifiers */ /* identifiers */
[a-zA-Z_][a-zA-Z_0-9]* { [a-zA-Z_][a-zA-Z_0-9]* {
*yylval = new IdentifierNode(yytext);
return IDENTIFIER; return IDENTIFIER;
} }

View File

@ -91,7 +91,9 @@ static YYSTYPE parse_result;
%% %%
program: program_items { program: program_items {
} $$ = new ProgramNode();
$$->addChildren($1);
}
; ;
program_items: /* empty */ program_items: /* empty */
@ -187,7 +189,9 @@ type: primitive_type
statement: expression SEMICOLON statement: expression SEMICOLON
| return_statement | return_statement
| c_statement | c_statement {
$$ = $1;
}
; ;
return_statement: RETURN expression SEMICOLON return_statement: RETURN expression SEMICOLON
@ -204,8 +208,8 @@ lvalue: IDENTIFIER
; ;
c_statement: C LPAREN STRING_LITERAL RPAREN SEMICOLON { c_statement: C LPAREN STRING_LITERAL RPAREN SEMICOLON {
printf("c_statement: '%s'\n", $3->getString().c_str()); $$ = new CStatementNode($3->getString());
} }
; ;
%% %%