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 committed 2010-01-13 20:48:37 +00:00
1 parent 8333a037c9
commit 55b80e8f09
5 files changed
+82 -18

No files matched your search

+31 -5
View File
@@ -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;
}
+8 -4
View File
@@ -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());
}
;
%%