Created
April 6, 2015 22:20
-
-
Save halldorel/b24e182b933d14f69230 to your computer and use it in GitHub Desktop.
Bjakk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
/* C/C++ skilgreiningar */ | |
#include <math.h> | |
#include <stdio.h> | |
#include <iostream> | |
#include <cstdlib> | |
#include <string> | |
#include <map> | |
//#include "Expression.h" | |
#define YYSTYPE_IS_DECLARED | |
using namespace std; | |
typedef struct { | |
string str; | |
double val; | |
string readableToken; | |
} YYSTYPE; | |
int yyerror(const char *s) { | |
printf("Villa: %s", s); | |
return 0; | |
} | |
int yylex(); | |
int yyline(); | |
int yycolumn(); | |
int yyparse(); | |
int token; | |
int varCount = 0; | |
map<string,int> vars; | |
int currentLabel = 1; | |
void emit( string s ) | |
{ | |
printf("%s\n", s.c_str()); | |
} | |
int createLabel() | |
{ | |
return currentLabel++; | |
} | |
%} | |
/* Bison skilgreiningar */ | |
%token IF | |
%token ELSIF | |
%token ELSE | |
%token VAR | |
%token WHILE | |
%token RETURN | |
%token OPERATOR | |
%token <str> STRING | |
%token DIGIT | |
%token <val> NOM | |
%token <str> NAME | |
%token <str> LITERAL | |
%token WS | |
%token ERR | |
%left '|' | |
%left '&' | |
%left '+' '-' | |
%left '*' '/' '%' | |
%left '=' | |
%left UMINUS | |
%left OPERATOR | |
%right RETURN | |
%% | |
/* Mállýsing með aðgerðum */ | |
start | |
: { emit("\"asdf.mexe\" = main in \n!{{"); } program { emit("}}*BASIS;"); } | |
; | |
program | |
: program function | |
| function | |
; | |
function | |
: { varCount = 0; vars.clear(); } NAME '(' args ')' | |
{ cout << "Blessaður" << endl; | |
char *varCountString; | |
sprintf(varCountString, "%d", varCount); | |
emit("#\"" + $2 + "\"[f"+ varCountString +"]"); } | |
'{' decl exprs '}' { | |
emit("(Return)"); | |
emit("];"); | |
} | |
; | |
decl | |
: VAR namelist ';' | |
| | |
; | |
namelist | |
: NAME ',' namelist | |
| NAME | |
; | |
expr | |
: NAME | |
| NAME '=' expr | |
| NAME '(' exprlist ')' | |
| RETURN expr | |
| '-' expr %prec UMINUS | |
| expr OPERATOR expr | |
| LITERAL | |
| '(' expr ')' | |
| ifexpr | |
| WHILE '(' expr ')' body | |
; | |
exprlist | |
: expr ',' exprlist | |
| expr | |
; | |
ifexpr | |
: IF '(' expr ')' body elseexpr | |
; | |
elseexpr | |
: ELSIF '(' expr ')' body elseexpr | |
| ELSE '(' expr ')' body | |
| /* empty */ | |
; | |
body | |
: '{' exprs '}' | |
; | |
exprs | |
: expr ';' exprs | |
| /* empty */ | |
; | |
args | |
: NAME ',' args { vars[$1] = varCount++; } | |
| NAME { vars[$1] = varCount++; } | |
| /* empty */ | |
; | |
%% | |
int main( int argc, char **argv ) | |
{ | |
int results = yyparse(); | |
if(results == 0) { | |
printf("Húrra! Þér tókst að smíða laukrétt NanoMorpho forrit!\n"); | |
} | |
else { | |
printf("\n\nVilla hefir komið upp.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment