Use strings

This commit is contained in:
Dane Johnson 2021-12-08 14:58:36 -06:00
parent 1c3233d272
commit 148abaf5ab
4 changed files with 18 additions and 14 deletions

View File

@ -1,5 +1,6 @@
%{
#include <stack>
#include <string>
#include "syntax.h"
#include "parser.h"
using namespace std;
@ -36,12 +37,12 @@ if (s.top() == yyleng) {
}
}
#[^\n]* // Eat comments
\"[^"]*\" yylval.sym = yytext; return STRING;
\"[^"]*\" yylval.sym = new string(yytext); return STRING;
if return IF;
else return ELSE;
elif return ELIF;
{digit}+|{digit}*\.{digit}+ yylval.num = atof(yytext); return NUM;
{letter}({letter}|{digit}|[?.-])* yylval.sym = yytext; return ID;
{letter}({letter}|{digit}|[?.-])* yylval.sym = new string(yytext); return ID;
"<-" return GETS;
"->" return MAPS;
".." return CAT;

View File

@ -1,5 +1,7 @@
%{
#include <cstdio>
#include <string>
#include "syntax.h"
@ -7,13 +9,14 @@ using namespace std;
int yylex();
void yyerror(const char* p) { fprintf(stderr, p); }
%}
%define parse.trace
%union {
float num;
char *sym;
std::string *sym;
Node *expr;
NodeList *exprlist;
}
@ -29,7 +32,7 @@ void yyerror(const char* p) { fprintf(stderr, p); }
%right GETS
%left CAT
%left '+' '-'
%left '/' '*'n
%left '/' '*'
%left MAPS
%nterm <expr> expr
@ -62,7 +65,7 @@ param: ID | '_' | // null production;
funcall: ID '(' exprlist ')' { print_expression_list($3); }
exprlist: exprlist ',' expr { $1 -> push_back($3); $$ = $1; }
| expr { $$ = new NodeList(); $$ -> push_back($1); }
| { $$ = NULL; } // null production;
| { $$ = new NodeList(); } // null production;
conditional: IF expr block elifs
| IF expr block elifs ELSE block;

View File

@ -6,12 +6,12 @@
using namespace std;
IdNode::IdNode(char * id) {
this->id = string(id);
IdNode::IdNode(string *id) {
this->id = id;
}
void IdNode::display() {
cout << id;
cout << *id;
}
NumNode::NumNode(float num) {
@ -22,8 +22,8 @@ void NumNode::display() {
}
StringNode::StringNode(char *str) {
this->stringVal = str;
StringNode::StringNode(string* stringVal) {
this->stringVal = stringVal;
}
void StringNode::display() {

View File

@ -11,9 +11,9 @@ public:
class IdNode : public Node {
private:
std::string id;
std::string *id;
public:
IdNode(char*);
IdNode(std::string*);
virtual void display();
};
@ -27,9 +27,9 @@ public:
class StringNode : public Node {
private:
char* stringVal;
std::string* stringVal;
public:
StringNode(char*);
StringNode(std::string*);
virtual void display();
};