This commit is contained in:
2021-11-26 12:14:07 -06:00
parent caabe8eb52
commit acf66e1816
10 changed files with 30 additions and 27 deletions

18
src/Makefile Normal file
View File

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

7
src/deelang.c Normal file
View File

@@ -0,0 +1,7 @@
#include "lexer.h"
#include "parser.h"
void main() {
yydebug = 1;
yyparse();
}

67
src/lexer.l Normal file
View File

@@ -0,0 +1,67 @@
%{
#include "parser.h"
int stack[100]; // Max indentation depth
int sp = -1;
int peek();
void push(int a);
void pop();
#define YY_USER_INIT push(0); BEGIN(freshline);
%}
%option noyywrap
%x freshline
digit [0-9]
letter [A-Za-z]
%%
<freshline>""/[^\t ] {
if (peek() == 0) {
BEGIN(0);
} else {
pop(); yyless(0); return DEDENT;
}
}
<freshline>[ \t]+ {
if (peek() == yyleng) {
BEGIN(0); // Same indentation, continue
} else if (peek() > yyleng) {
pop(); yyless(0); return DEDENT;
// Same rule again until the stack is even
} else {
push(yyleng); BEGIN(0); return INDENT;
}
}
#[^\n]* // Eat comments
\"[^"]*\" yylval.sym = 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;
"<-" 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);
%%
int peek() {
printf("Peek @ %d\n", stack[sp]);
return stack[sp];
}
void push(int a) {
printf("Push @ %d\n", a);
sp++;
stack[sp] = a;
}
void pop() {
printf("Pop! Sp @ %d\n", sp);
sp--;
}

61
src/parser.y Normal file
View File

@@ -0,0 +1,61 @@
%{
#include <stdio.h>
#include "syntax.h"
int yylex();
int yyerror(const char* p) { fprintf(stderr, p); }
%}
%define parse.trace
%union {
char* sym;
float num;
}
%token <sym> ID
%token STOP
%token <num> NUM
%token STRING
%token INDENT DEDENT
%token IF ELIF ELSE
%right GETS
%left CAT
%left '+' '-'
%left '/' '*'
%left MAPS
%%
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
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '(' expr ')'
| ID | NUM | STRING;
funcdef: param MAPS expr | param MAPS block;
param: ID | '_' | // null production;
funcall: ID '(' exprlist ')'
exprlist: exprlist ',' expr | expr | // 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
%%

6
src/syntax.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef SYNTAX_H
#define SYNTAX_H
#endif /* SYNTAX_H */