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:
parent
8333a037c9
commit
55b80e8f09
12
nodes/CStatementNode.cc
Normal file
12
nodes/CStatementNode.cc
Normal 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");
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
|
||||
#include "Node.h"
|
||||
#include <vector>
|
||||
#include "Node.h"
|
||||
#include "util/refptr.h"
|
||||
using namespace std;
|
||||
|
||||
|
38
nodes/Node.h
38
nodes/Node.h
@ -2,6 +2,7 @@
|
||||
#ifndef NODE_H
|
||||
#define NODE_H NODE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@ -15,31 +16,52 @@ class Node
|
||||
void addChildren(refptr<Node> other);
|
||||
std::vector< refptr<Node> > & getChildren() { return m_children; }
|
||||
|
||||
virtual uint64_t getInteger() { return 0x0ULL; }
|
||||
virtual std::string getString() { return ""; }
|
||||
virtual double getDouble() { return m_double; }
|
||||
virtual uint64_t getInteger() { return m_integer; }
|
||||
virtual std::string getString() { return m_string; }
|
||||
virtual void process(FILE * out) { }
|
||||
|
||||
protected:
|
||||
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
|
||||
{
|
||||
public:
|
||||
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
|
||||
{
|
||||
public:
|
||||
StringNode(const std::string & str) { m_string = str; }
|
||||
std::string getString() { return m_string; }
|
||||
protected:
|
||||
std::string m_string;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -4,12 +4,31 @@
|
||||
|
||||
%{
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include "parser.h"
|
||||
#include "parser.tab.hh"
|
||||
#include <string>
|
||||
using namespace std;
|
||||
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
|
||||
@ -47,17 +66,23 @@ static string build_string;
|
||||
\( return LPAREN;
|
||||
\) return RPAREN;
|
||||
|
||||
/* literals */
|
||||
[0-9]+ {
|
||||
/* numeric literals */
|
||||
[0-9][0-9_]* {
|
||||
*yylval = new IntegerNode(parseInt(yytext, 10));
|
||||
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;
|
||||
}
|
||||
0b[01]+ {
|
||||
0b[01][01_]* {
|
||||
*yylval = new IntegerNode(parseInt(yytext + 2, 2));
|
||||
return INT_LITERAL;
|
||||
}
|
||||
[0-9]*\.[0-9]+([eE]-?[0-9]+)? {
|
||||
double val;
|
||||
(void) sscanf(yytext, "%lf", &val);
|
||||
*yylval = new DoubleNode(val);
|
||||
return REAL_LITERAL;
|
||||
}
|
||||
|
||||
@ -84,6 +109,7 @@ struct return STRUCT;
|
||||
|
||||
/* identifiers */
|
||||
[a-zA-Z_][a-zA-Z_0-9]* {
|
||||
*yylval = new IdentifierNode(yytext);
|
||||
return IDENTIFIER;
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,9 @@ static YYSTYPE parse_result;
|
||||
%%
|
||||
|
||||
program: program_items {
|
||||
}
|
||||
$$ = new ProgramNode();
|
||||
$$->addChildren($1);
|
||||
}
|
||||
;
|
||||
|
||||
program_items: /* empty */
|
||||
@ -187,7 +189,9 @@ type: primitive_type
|
||||
|
||||
statement: expression SEMICOLON
|
||||
| return_statement
|
||||
| c_statement
|
||||
| c_statement {
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
return_statement: RETURN expression SEMICOLON
|
||||
@ -204,8 +208,8 @@ lvalue: IDENTIFIER
|
||||
;
|
||||
|
||||
c_statement: C LPAREN STRING_LITERAL RPAREN SEMICOLON {
|
||||
printf("c_statement: '%s'\n", $3->getString().c_str());
|
||||
}
|
||||
$$ = new CStatementNode($3->getString());
|
||||
}
|
||||
;
|
||||
|
||||
%%
|
||||
|
Loading…
x
Reference in New Issue
Block a user