This is a rust project now, let's end the segregation

This commit is contained in:
Dane Johnson 2024-11-12 13:56:37 -06:00
parent 0fddcbd645
commit 201d7d2594
14 changed files with 1 additions and 267 deletions

5
.gitignore vendored
View File

@ -1,4 +1 @@
deelang
*.o
parser.h
lexer.h
target/*

View File

1
rust/.gitignore vendored
View File

@ -1 +0,0 @@
target/*

View File

@ -1,20 +0,0 @@
.PHONY: all clean
all: deelang
CC = g++
CPPFLAGS = -g
lexer.o lexer.h: parser.h lexer.l
flex -o lexer.cpp --header-file=lexer.h lexer.l
$(CC) -c lexer.cpp
rm lexer.cpp
parser.h parser.o: parser.y
bison -o parser.cpp --header=parser.h parser.y
$(CC) -c parser.cpp
rm parser.cpp
deelang: deelang.cpp lexer.o parser.o syntax.o
$(CC) $(CPPFLAGS) -o $@ $^
clean:
rm -rf *.o parser.h lexer.h deelang

View File

@ -1,11 +0,0 @@
#include "syntax.h"
#include "lexer.h"
#include "parser.h"
using namespace std;
int main() {
yyparse();
return 0;
}

View File

@ -1,54 +0,0 @@
%{
#include <stack>
#include <string>
#include "syntax.h"
#include "parser.h"
using namespace std;
stack<int> s;
#define YY_USER_INIT s.push(0); BEGIN(freshline);
%}
%option noyywrap
%x freshline
digit [0-9]
letter [A-Za-z]
%%
<freshline>""/[^\t ] {
if (s.top() == 0) {
BEGIN(0);
} else {
s.pop(); yyless(0); return DEDENT;
}
}
<freshline>[ \t]+ {
if (s.top() == yyleng) {
BEGIN(0); // Same indentation, continue
} else if (s.top() > yyleng) {
s.pop(); yyless(0); return DEDENT;
// Same rule again until the stack is even
} else {
s.push(yyleng); BEGIN(0); return INDENT;
}
}
#[^\n]* // Eat comments
\"[^"]*\" 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 = new string(yytext); return ID;
"<-" return GETS;
"->" return MAPS;
".." return CAT;
[(){}.,*/+-] return yytext[0];
[\t ] // Eat whitespace not first on a line
"/"\n // Eat newlines ending in /
[\n;] BEGIN(freshline); return STOP;
. fprintf(stderr, "Scanning error!\nOffender: %s\n", yytext); exit(1);
%%

View File

@ -1,78 +0,0 @@
%{
#include <cstdio>
#include <string>
#include "syntax.h"
using namespace std;
int yylex();
void yyerror(const char* p) { fprintf(stderr, p); }
%}
%define parse.trace
%union {
float num;
std::string *sym;
Node *expr;
NodeList *exprlist;
}
%token <sym> ID
%token <num> NUM
%token <sym> STRING
%token STOP
%token INDENT DEDENT
%token IF ELIF ELSE
%right GETS
%left CAT
%left '+' '-'
%left '/' '*'
%left MAPS
%nterm <expr> expr
%nterm <exprlist> exprlist
%%
program: statements | statements statement;
statements: statements statement STOP
| statements STOP
| // null production ;
statement: assignment | funcall | conditional;
assignment: ID GETS expr;
assignments: assignments assignment STOP | // null production;
expr: funcdef
| funcall
| objdef
| expr CAT expr { $$ = new OpNode(CAT, $1, $3); }
| expr '+' expr { $$ = new OpNode('+', $1, $3); }
| expr '-' expr { $$ = new OpNode('-', $1, $3); }
| expr '*' expr { $$ = new OpNode('*', $1, $3); }
| expr '/' expr { $$ = new OpNode('/', $1, $3); }
| '(' expr ')' {$$ = $2;}
| ID {$$ = new IdNode($1);}
| NUM {$$ = new NumNode($1);}
| STRING {$$ = new StringNode($1);};
funcdef: param MAPS expr | param MAPS block;
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); }
| { $$ = new NodeList(); } // null production;
conditional: IF expr block elifs
| IF expr block elifs ELSE block;
elifs: ELIF expr block elifs| // null production;
objdef: '{' block_assignments '}' | '{' '}'
block: STOP INDENT statement statements DEDENT
block_assignments: STOP INDENT assignments DEDENT
%%

View File

@ -1,49 +0,0 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "syntax.h"
using namespace std;
IdNode::IdNode(string *id) {
this->id = id;
}
void IdNode::display() {
cout << *id;
}
NumNode::NumNode(float num) {
this->num = num;
}
void NumNode::display() {
cout << num;
}
StringNode::StringNode(string* stringVal) {
this->stringVal = stringVal;
}
void StringNode::display() {
cout << *stringVal;
}
OpNode::OpNode(int op, Node *first, Node *second) {
this->op = op;
this->first = first;
this->second = second;
}
void OpNode::display() {
first->display();
cout << " " << (char)op << " ";
second->display();
}
void print_expression_list(NodeList *list) {
for (Node *expr : *list) {
expr->display();
}
}

View File

@ -1,50 +0,0 @@
#ifndef SYNTAX_H
#define SYNTAX_H
#include <vector>
#include <string>
class Node {
public:
virtual void display() = 0;
};
class IdNode : public Node {
private:
std::string *id;
public:
IdNode(std::string*);
virtual void display();
};
class NumNode : public Node {
private:
float num;
public:
NumNode(float);
virtual void display();
};
class StringNode : public Node {
private:
std::string* stringVal;
public:
StringNode(std::string*);
virtual void display();
};
class OpNode : public Node {
private:
int op;
Node *first;
Node *second;
public:
OpNode(int, Node*, Node*);
virtual void display();
};
class NodeList : public std::vector<Node*>{};
void print_expression_list(NodeList *);
#endif /* SYNTAX_H */