Fix bugs
Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar> Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
parent
63a239fce3
commit
18ef547882
|
@ -1,26 +1,20 @@
|
|||
# Versión mínima requerida de CMake:
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
# Nombre del proyecto, y lenguaje usado (C, en este caso):
|
||||
project(BFBCompiler C)
|
||||
|
||||
# Compilar el parser con Bison:
|
||||
add_custom_command(
|
||||
OUTPUT ../src/frontend/syntactic-analysis/bison-parser.c ../src/frontend/syntactic-analysis/bison-parser.h
|
||||
# COMMAND bison -d ../src/frontend/syntactic-analysis/bison-grammar.y -o ../src/frontend/syntactic-analysis/bison-parser.c)
|
||||
COMMAND bison -Wcounterexamples -d ../src/frontend/syntactic-analysis/bison-grammar.y -o ../src/frontend/syntactic-analysis/bison-parser.c)
|
||||
# COMMAND bison -t -d ../src/frontend/syntactic-analysis/bison-grammar.y -o ../src/frontend/syntactic-analysis/bison-parser.c)
|
||||
|
||||
# Seleccionar estrategia según el compilador de C disponible en el sistema:
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||
message(NOTICE "El compilador de C es GCC.")
|
||||
|
||||
# Opciones para GCC:
|
||||
add_compile_options(-static-libgcc)
|
||||
add_compile_options(-std=gnu99)
|
||||
add_compile_options(-O3)
|
||||
|
||||
# Compilar el scanner con Flex:
|
||||
add_custom_command(
|
||||
OUTPUT ../src/frontend/lexical-analysis/flex-scanner.c
|
||||
COMMAND flex -o ../src/frontend/lexical-analysis/flex-scanner.c ../src/frontend/lexical-analysis/flex-patterns.l
|
||||
|
@ -30,10 +24,6 @@ if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
|||
elseif (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
|
||||
message(NOTICE "El compilador de C es Microsoft Visual Studio.")
|
||||
|
||||
# Opciones para MSVC:
|
||||
# add_compile_options(...)
|
||||
|
||||
# Compilar el scanner con Flex, usando compatibilidad con Microsoft Windows:
|
||||
add_custom_command(
|
||||
OUTPUT ../src/frontend/lexical-analysis/flex-scanner.c
|
||||
COMMAND flex --wincompat -o ../src/frontend/lexical-analysis/flex-scanner.c ../src/frontend/lexical-analysis/flex-patterns.l
|
||||
|
@ -45,7 +35,6 @@ else ()
|
|||
|
||||
endif ()
|
||||
|
||||
# Especificar punto de entrada del proyecto y códigos fuente (extensión *.c):
|
||||
add_executable(BFBCompiler
|
||||
src/main.c
|
||||
src/backend/code-generation/generator.c
|
||||
|
@ -56,8 +45,14 @@ add_executable(BFBCompiler
|
|||
src/frontend/syntactic-analysis/bison-actions.c
|
||||
src/frontend/syntactic-analysis/bison-parser.c)
|
||||
|
||||
# Linkear el proyecto y sus librerías:
|
||||
target_link_libraries(BFBCompiler)
|
||||
include(CheckFunctionExists)
|
||||
check_function_exists(pow POW_FUNCTION_EXISTS)
|
||||
# check_symbol_exists(pow "math.h" POW_FUNCTION_EXISTS)
|
||||
if (NOT POW_FUNCTION_EXISTS)
|
||||
target_link_libraries(BFBCompiler m)
|
||||
else ()
|
||||
target_link_libraries(BFBCompiler)
|
||||
endif ()
|
||||
|
||||
file(GLOB_RECURSE correct_use_cases "doc/examples/correct/use-case-*.b" )
|
||||
file(GLOB_RECURSE incorrect_use_cases "doc/examples/incorrect/use-case-*.b" )
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2022 Agustín Golmar \
|
||||
Copyright (c) 2022 Santiago Lo Coco \
|
||||
Copyright (c) 2022 Juan Martín Barmasch \
|
||||
Copyright (c) 2022 Ezequiel Bellver
|
||||
|
|
|
@ -2,4 +2,3 @@ fun g = (1-x)^(x);
|
|||
fun f = x^2;
|
||||
fun h = x/(x-2);
|
||||
fun j = 1/x;
|
||||
printCSV(["g", "f", "h", "j"], [[g(1), f(1), h(1) , j(1), gofohoj(1)]], "example.csv");
|
|
@ -1,6 +1,6 @@
|
|||
fun f = x^2;
|
||||
fun g = derivative f in (x);
|
||||
fun h = f(x) - int f (0 ~ 1);
|
||||
fun h = f(x) - int g (0 ~ 1);
|
||||
for (var i = 0; i <= 1; i += 0.1) {
|
||||
print(g(i));
|
||||
print(h(i));
|
||||
}
|
2
run.sh
2
run.sh
|
@ -1,6 +1,6 @@
|
|||
#!/bin/sh
|
||||
|
||||
if [ ! -d "bin" ]; then
|
||||
if [ ! -d "bin" ] || [ "$2" = 'D' ]; then
|
||||
sh compile.sh
|
||||
fi
|
||||
|
||||
|
|
|
@ -1,21 +1,32 @@
|
|||
#include "calculator.h"
|
||||
#include "math.h"
|
||||
|
||||
/**
|
||||
* Implementación de "calculator.h".
|
||||
*/
|
||||
|
||||
int Add(const int leftAddend, const int rightAddend) {
|
||||
double Add(const double leftAddend, const double rightAddend) {
|
||||
return leftAddend + rightAddend;
|
||||
}
|
||||
|
||||
int Subtract(const int minuend, const int subtract) {
|
||||
double Subtract(const double minuend, const double subtract) {
|
||||
return minuend - subtract;
|
||||
}
|
||||
|
||||
int Multiply(const int multiplicand, const int multiplier) {
|
||||
double Multiply(const double multiplicand, const double multiplier) {
|
||||
return multiplicand * multiplier;
|
||||
}
|
||||
|
||||
int Divide(const int dividend, const int divisor) {
|
||||
double Divide(const double dividend, const double divisor) {
|
||||
return dividend / divisor;
|
||||
}
|
||||
|
||||
double Power(const double base, const double exponent) {
|
||||
if (base == 0 && exponent == 0)
|
||||
return 0;
|
||||
return pow(base, exponent);
|
||||
}
|
||||
|
||||
// double (* function) (double) declareFunction(char * expression) {
|
||||
|
||||
// }
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
#ifndef CALCULATOR_HEADER
|
||||
#define CALCULATOR_HEADER
|
||||
|
||||
int Add(const int leftAddend, const int rightAddend);
|
||||
double Add(const double leftAddend, const double rightAddend);
|
||||
|
||||
int Subtract(const int minuend, const int subtract);
|
||||
double Subtract(const double minuend, const double subtract);
|
||||
|
||||
int Multiply(const int multiplicand, const int multiplier);
|
||||
double Multiply(const double multiplicand, const double multiplier);
|
||||
|
||||
int Divide(const int dividend, const int divisor);
|
||||
double Divide(const double dividend, const double divisor);
|
||||
|
||||
double Power(const double base, const double exponent);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -36,6 +36,6 @@ void LogErrorRaw(const char * const format, ...) {
|
|||
void LogInfo(const char * const format, ...) {
|
||||
va_list arguments;
|
||||
va_start(arguments, format);
|
||||
Log(stdout, "[INFO ] ", format, "\n", arguments);
|
||||
Log(stdout, "[INFO] ", format, "\n", arguments);
|
||||
va_end(arguments);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ TokenID DoublePatternAction(const char * lexeme) {
|
|||
|
||||
TokenID StringPatternAction(const char * lexeme) {
|
||||
// LogDebug("StringPatternAction: '%s'.", lexeme);
|
||||
return STRING;
|
||||
return ID;
|
||||
}
|
||||
|
||||
TokenID EscapeStringPatternAction(const char * lexeme) {
|
||||
|
|
|
@ -67,10 +67,9 @@ typedef enum TokenID {
|
|||
ERR,
|
||||
TAYLOR,
|
||||
ESCAPESTRING,
|
||||
PRINTCSV,
|
||||
DOUBLE,
|
||||
INTEGER,
|
||||
STRING
|
||||
ID
|
||||
} TokenID;
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
/* Patrones reutilizables. */
|
||||
crlf \r\n
|
||||
digit [0-9]
|
||||
char [a-zA-Z]
|
||||
characters [a-zA-Z., ]
|
||||
char [[:alpha:]_]
|
||||
characters [[:print:]]
|
||||
decimal [.]
|
||||
endline \n
|
||||
whitespace [ \f\n\r\t\v]
|
||||
|
@ -76,8 +76,6 @@ whitespace [ \f\n\r\t\v]
|
|||
"var" { return VAR; }
|
||||
"err" { return ERR; }
|
||||
"taylor" { return TAYLOR; }
|
||||
"printCSV" { return PRINTCSV; }
|
||||
|
||||
|
||||
{digit}+{decimal}{digit}+ { return DoublePatternAction(yytext); }
|
||||
|
||||
|
|
|
@ -55,10 +55,9 @@
|
|||
%token ERR
|
||||
%token TAYLOR
|
||||
%token ESCAPESTRING
|
||||
%token PRINTCSV
|
||||
%token DOUBLE
|
||||
%token INTEGER
|
||||
%token STRING
|
||||
%token ID
|
||||
|
||||
// Reglas de asociatividad y precedencia (de menor a mayor):
|
||||
|
||||
|
@ -66,9 +65,11 @@
|
|||
%precedence ELSE
|
||||
|
||||
%left GREATER GREATER_EQUAL LESSER LESSER_EQUAL EQUAL NOT_EQUAL
|
||||
%left ADD SUB NOT
|
||||
%left ADD SUB
|
||||
%right NOT
|
||||
%left MUL DIV AND OR
|
||||
%left POW
|
||||
%left SQUOTE
|
||||
|
||||
%%
|
||||
|
||||
|
@ -82,7 +83,6 @@ statement: evaluate SEMICOLON
|
|||
| if_statement
|
||||
| for_statement
|
||||
| while_statement
|
||||
| printCSV SEMICOLON
|
||||
| print SEMICOLON
|
||||
| declare_variable SEMICOLON
|
||||
| assign_variable SEMICOLON
|
||||
|
@ -92,7 +92,7 @@ expression: positive_constant
|
|||
| evaluate
|
||||
| integrate
|
||||
| derivative
|
||||
| STRING
|
||||
| ID
|
||||
| expression ADD expression
|
||||
| expression SUB expression
|
||||
| expression MUL expression
|
||||
|
@ -104,8 +104,8 @@ expression: positive_constant
|
|||
|
||||
domain: expression LESSER expression
|
||||
| expression LESSER_EQUAL expression
|
||||
| expression GREATER expression
|
||||
| expression GREATER_EQUAL expression
|
||||
| expression GREATER expression
|
||||
| expression EQUAL expression
|
||||
| expression NOT_EQUAL expression
|
||||
| expression LESSER expression LESSER expression
|
||||
|
@ -114,12 +114,7 @@ domain: expression LESSER expression
|
|||
| expression LESSER_EQUAL expression LESSER_EQUAL expression
|
||||
;
|
||||
|
||||
boolean: expression LESSER expression
|
||||
| expression LESSER_EQUAL expression
|
||||
| expression GREATER expression
|
||||
| expression GREATER_EQUAL expression
|
||||
| expression EQUAL expression
|
||||
| expression NOT_EQUAL expression
|
||||
boolean: domain
|
||||
| boolean OR boolean
|
||||
| boolean AND boolean
|
||||
| OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS
|
||||
|
@ -130,8 +125,8 @@ positive_constant: DOUBLE
|
|||
| INTEGER
|
||||
;
|
||||
|
||||
declare_function: FUNCTION STRING ASSIGN function
|
||||
| FUNCTION STRING ASSIGN OPEN_BRACES functions CLOSE_BRACES
|
||||
declare_function: FUNCTION ID ASSIGN function
|
||||
| FUNCTION ID ASSIGN OPEN_BRACES functions CLOSE_BRACES
|
||||
;
|
||||
|
||||
function: expression
|
||||
|
@ -144,16 +139,16 @@ functions: function
|
|||
| function COMMA functions
|
||||
;
|
||||
|
||||
composite: STRING COMPOSITE STRING
|
||||
| composite COMPOSITE STRING
|
||||
composite: ID COMPOSITE ID
|
||||
| composite COMPOSITE ID
|
||||
;
|
||||
|
||||
expressions: expression
|
||||
| expression COMMA expressions
|
||||
;
|
||||
|
||||
strings: STRING
|
||||
| STRING COMMA strings
|
||||
ids: ID
|
||||
| ID COMMA ids
|
||||
;
|
||||
|
||||
numArray: OPEN_BRACKETS expressions CLOSE_BRACKETS
|
||||
|
@ -166,7 +161,7 @@ numArrays: numArray
|
|||
arrayNumArray: OPEN_BRACKETS numArrays CLOSE_BRACKETS
|
||||
;
|
||||
|
||||
stringArray: OPEN_BRACKETS strings CLOSE_BRACKETS
|
||||
idArray: OPEN_BRACKETS ids CLOSE_BRACKETS
|
||||
;
|
||||
|
||||
escapeString: ESCAPESTRING
|
||||
|
@ -180,18 +175,18 @@ escapeStrings: escapeString
|
|||
escapeStringArray: OPEN_BRACKETS escapeStrings CLOSE_BRACKETS
|
||||
;
|
||||
|
||||
evaluate: EVALUATE STRING IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE STRING IN numArray
|
||||
| STRING OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE stringArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE stringArray IN numArray
|
||||
| stringArray OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
evaluate: EVALUATE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE ID IN numArray
|
||||
| ID OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| EVALUATE idArray IN numArray
|
||||
| idArray OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
;
|
||||
|
||||
integrate: INTEGRATE STRING BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE STRING OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE stringArray BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE stringArray OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
integrate: INTEGRATE ID BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE ID OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE idArray BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| INTEGRATE idArray OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS
|
||||
| integrate ERR positive_constant
|
||||
;
|
||||
|
||||
|
@ -207,12 +202,12 @@ statements: statement
|
|||
| statement statements
|
||||
;
|
||||
|
||||
derivative: DERIVATIVE STRING IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE INTEGER STRING IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| STRING squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE stringArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE INTEGER stringArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| stringArray squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
derivative: DERIVATIVE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE INTEGER ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| ID squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| DERIVATIVE INTEGER idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| idArray squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
|
||||
| derivative ERR positive_constant
|
||||
;
|
||||
|
||||
|
@ -228,26 +223,23 @@ for_statement: FOR OPEN_PARENTHESIS declare_variable SEMICOLON boolean SEMICOLON
|
|||
| FOR OPEN_PARENTHESIS SEMICOLON boolean SEMICOLON assign_variable CLOSE_PARENTHESIS block
|
||||
;
|
||||
|
||||
taylor: TAYLOR INTEGER STRING
|
||||
taylor: TAYLOR INTEGER ID
|
||||
;
|
||||
|
||||
while_statement: WHILE OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block
|
||||
;
|
||||
|
||||
declare_variable: VAR STRING
|
||||
declare_variable: VAR ID
|
||||
| declare_variable ASSIGN expression
|
||||
;
|
||||
|
||||
printCSV: PRINTCSV OPEN_PARENTHESIS escapeStringArray COMMA arrayNumArray COMMA escapeString CLOSE_PARENTHESIS
|
||||
;
|
||||
|
||||
assign_variable: STRING ADD_ASSIGN expression
|
||||
| STRING SUB_ASSIGN expression
|
||||
| STRING MUL_ASSIGN expression
|
||||
| STRING DIV_ASSIGN expression
|
||||
| STRING INCREMENT
|
||||
| STRING DECREMENT
|
||||
| STRING ASSIGN expression
|
||||
assign_variable: ID ADD_ASSIGN expression
|
||||
| ID SUB_ASSIGN expression
|
||||
| ID MUL_ASSIGN expression
|
||||
| ID DIV_ASSIGN expression
|
||||
| ID INCREMENT
|
||||
| ID DECREMENT
|
||||
| ID ASSIGN expression
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
|
@ -3,27 +3,22 @@
|
|||
#include "backend/support/shared.h"
|
||||
#include "frontend/syntactic-analysis/bison-parser.h"
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
//Estado de la aplicación.
|
||||
CompilerState state;
|
||||
|
||||
// Punto de entrada principal del compilador.
|
||||
const int main(const int argumentCount, const char ** arguments) {
|
||||
|
||||
// #ifdef YYDEBUG
|
||||
// yydebug = 1;
|
||||
// #endif
|
||||
|
||||
// Inicializar estado de la aplicación.
|
||||
state.result = 0;
|
||||
state.succeed = false;
|
||||
|
||||
// Mostrar parámetros recibidos por consola.
|
||||
for (int i = 0; i < argumentCount; ++i) {
|
||||
LogInfo("Argumento %d: '%s'", i, arguments[i]);
|
||||
}
|
||||
|
||||
// Compilar el programa de entrada.
|
||||
LogInfo("Compilando...\n");
|
||||
const int result = yyparse();
|
||||
switch (result) {
|
||||
|
|
Loading…
Reference in New Issue