Skip to content

Commit

Permalink
Merge pull request #1 from JackCloudman/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JackCloudman authored May 14, 2019
2 parents 0825a2b + 20f822b commit 0dee66c
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Lala
lex.yy.c
y.tab.c
y.tab.h
.idea
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Gram=Objects/y.tab.c Objetcs/y.tab.h

all: $(Gram) Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c
@gcc -I include -o Lala Objects/y.tab.c Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c
@echo Compiled

$(Gram): Objects/LLang.y
@yacc -d Objects/LLang.y
@mv *.tab.* Objects/

Objects/lex.yy.c: Objects/LLang.l
@flex Objects/LLang.l
@mv lex.yy.c Objects/

clean:
@rm -f Objects/lex.yy.c Objects/*.tab.* Lala
@echo Clean
48 changes: 48 additions & 0 deletions Objects/LLang.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include "Lala.h"
#define YYSTYPE APLLObject
#include "y.tab.h"
extern LLIntObject* LLInt_Make(int);
extern LLStringObject* LLString_Make(char*);
void RmWs(char* str);
extern YYSTYPE yylval;
%}
/* Add your Flex definitions here */
/* Some definitions are already provided to you*/
op [-+*\/()]
ws [ \t]+
digits [0-9]
number (0|[1-9]+{digits}*)\.?{digits}*
STRING \"(\\.|[^"\\])*\"
%%
{number} {
int dato;
RmWs(yytext);
sscanf(yytext,"%d",&dato);
yylval=(LLObject*) LLInt_Make(dato);
return object;}
{op} |
\n {return *yytext;}
{ws} { /* Do nothing */ }
. { /* Do nothing */ }
{STRING} {
yylval = (LLObject*)LLString_Make(yytext);
return object;
}
%%
void RmWs(char* str) {
int i = 0, j = 0;
char temp[strlen(str) + 1];
strcpy(temp, str);
while (temp[i] != '\0') {
while (temp[i] == ' ')
i++;
str[j] = temp[i];
i++;
j++;
}
str[j] = '\0';
}
43 changes: 43 additions & 0 deletions Objects/LLang.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%{
#include <stdio.h>
#include "Lala.h"
#include "abstract.h"
void yyerror (char *s);
int yylex ();
void warning(char *s, char *t);
extern void init();
%}
%token object
%left '+' '-'
%left '*' '/'
%%
list:
| list'\n'
| list exp '\n' {LL_FUNC_PRINT($2,"\n");printf(">>> ");}
;
exp: object { $$ = $1;}
| exp '+' exp { $$ = LL_FUNC_ADD($1,$3); }
| exp '-' exp { $$ = LL_FUNC_SUB($1,$3); }
| exp '*' exp { $$ = LL_FUNC_MUL($1,$3);}
| exp '/' exp { $$ = LL_FUNC_DIV($1,$3); }
| '(' exp ')' { $$ = $2;}
;
%%
#include <stdio.h>
#include <ctype.h>
char *progname;
int main (int argc, char *argv[]){
init();
printf(">>> ");
progname=argv[0];
yyparse ();
return 0;
}
void yyerror (char *s) {
warning(s, (char *) 0);
}
void warning(char *s, char *t){
fprintf (stderr, "%s: %s", progname, s);
if(t)
fprintf (stderr, " %s", t);
}
Empty file added Objects/Object.c
Empty file.
58 changes: 58 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "abstract.h"
#include "Lala.h"
extern LLTypeObject* LLIntTypeObject;
// Implementaciones
LLObject* LL_FUNC_ADD(LLObject* a,LLObject* b) {
LLObject *result = 0;
if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)){
result = LLInt_ADD(a, b);
} else {
if ((a->ob_type == LLStringTypeObject)&& (b->ob_type == LLStringTypeObject)) {
result = LLString_CAT(a, b);
}else{
if(a->ob_type==LLStringTypeObject){
result = LLString_CAT(a,b->to_String(b));
}else if(b->ob_type==LLStringTypeObject){
result = LLString_CAT(a->to_String(a),b);
}
}
}
return result;
}
LLObject* LL_FUNC_SUB(LLObject* a,LLObject* b){
LLObject* result = 0;
if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) {
result = LLInt_SUB(a, b);
}
return result;
}
LLObject* LL_FUNC_MUL(LLObject* a,LLObject* b){
LLObject* result = 0;
if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) {
result = LLInt_MUL(a, b);
}else if ((a->ob_type == LLStringTypeObject)&&(b->ob_type == LLIntTypeObject)){
result = LLString_MUL(a,b);
}else if((a->ob_type == LLIntTypeObject)&&(b->ob_type == LLStringTypeObject)){
result = LLString_MUL(b,a);
}
return result;
}
LLObject* LL_FUNC_DIV(LLObject* a,LLObject* b){
LLObject* result = 0;
if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) {
result = LLInt_DIV(a, b);
}
return result;
}


int LL_FUNC_PRINT(LLObject* o,char* end){
if(o==0){
return 0;
}
o->print(o);
if(end!=NULL){
printf("%s",end);
}
return 0;
}
20 changes: 20 additions & 0 deletions Objects/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Lala.h"
extern LLTypeObject* LLIntTypeObject;
extern LLTypeObject* LLStringTypeObject;
extern LLTypeObject* LLListTypeObject;
void init(){
LLIntTypeObject = calloc(1,sizeof(LLTypeObject));
LLStringTypeObject = calloc(1,sizeof(LLTypeObject));
LLListTypeObject = calloc(1,sizeof(LLTypeObject));
char* c = 0;
c = malloc(sizeof(char)*13);
strcpy(c,"<type 'int'>\0");
LLIntTypeObject->name = c;
c = malloc(sizeof(char)*13);
strcpy(c,"<type 'str'>\0");
LLStringTypeObject->name = c;
c = malloc(sizeof(char)*14);
strcpy(c,"<type 'list'>\0");
LLListTypeObject->name = c;
return;
}
40 changes: 40 additions & 0 deletions Objects/intObject.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "intObject.h"

LLObject* LLInt_String(LLObject *o){
char* buffer = (char*)calloc(32,sizeof(char));
int j;
j = sprintf(buffer,"\"");
if(o!=NULL)
j += sprintf(buffer+j,"%d",((LLIntObject*)o)->o_val);
j+= sprintf(buffer+j,"\"");
buffer[j] = '\0';
return (LLObject*)LLString_Make(buffer);
}
int LLInt_print(LLObject *o){
printf("%d",((LLIntObject*)o)->o_val);
return 1;
}
LLIntObject* LLInt_Make(int val){
LLIntObject * result = 0;
result = calloc(1,sizeof(LLIntObject));
result->o_val = val;
result->print = LLInt_print;
result->to_String = LLInt_String;
result->ob_type = LLIntTypeObject;
return result;
}
LLObject* LLInt_ADD(LLObject* a,LLObject* b){
return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val+((LLIntObject*)b)->o_val);
}

LLObject* LLInt_SUB(LLObject* a,LLObject* b){
return (LLObject*)LLInt_Make((((LLIntObject*)a)->o_val)-(((LLIntObject*)b)->o_val));
}
LLObject* LLInt_MUL(LLObject* a,LLObject* b){
return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val*((LLIntObject*)b)->o_val);
}
LLObject* LLInt_DIV(LLObject* a,LLObject* b){
return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val/((LLIntObject*)b)->o_val);
}


Loading

0 comments on commit 0dee66c

Please sign in to comment.