Added division.
This commit is contained in:
parent
a57acc1176
commit
b4cd46a1e7
@ -37,6 +37,11 @@ ASTNumData exec_call(AST* ast) {
|
|||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
return n1 * n2;
|
return n1 * n2;
|
||||||
|
} else if (!strcmp(calldata->to, "/") && calldata->argc == 2) {
|
||||||
|
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
||||||
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
|
return n1 / n2;
|
||||||
}
|
}
|
||||||
return -1000;
|
return -1000;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
%token NEG
|
%token NEG
|
||||||
%token PLUS
|
%token PLUS
|
||||||
%token MULT
|
%token MULT
|
||||||
|
%token DIV
|
||||||
|
|
||||||
%token NL
|
%token NL
|
||||||
%type<ast> exp
|
%type<ast> exp
|
||||||
@ -91,4 +92,28 @@ exp:
|
|||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
| NUM DIV NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($3));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv));
|
||||||
|
}
|
||||||
|
| NEG NUM DIV NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($4));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv));
|
||||||
|
}
|
||||||
|
| NEG NUM DIV NEG NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$5));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv));
|
||||||
|
}
|
||||||
|
| NUM DIV NEG NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$4));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("/", 2, argv));
|
||||||
|
}
|
||||||
%%
|
%%
|
||||||
|
@ -3,16 +3,7 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#ifdef __has_include
|
|
||||||
#if __has_include("../../build/grammars/grammar.tab.h")
|
|
||||||
#include "../../build/grammars/grammar.tab.h"
|
#include "../../build/grammars/grammar.tab.h"
|
||||||
#else
|
|
||||||
#warn "Build resources not present!"
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#warn "Not sure whether build-time resources are present."
|
|
||||||
#include "../../build/grammars/grammar.tab.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern YYSTYPE yylval;
|
extern YYSTYPE yylval;
|
||||||
extern char* inp;
|
extern char* inp;
|
||||||
|
11
src/lexer.c
11
src/lexer.c
@ -28,22 +28,12 @@ double acc_float(int c) {
|
|||||||
inp++;
|
inp++;
|
||||||
|
|
||||||
while (isdigit(*inp)) {
|
while (isdigit(*inp)) {
|
||||||
// TODO:
|
|
||||||
// Accumulate as int, divide once at end.
|
|
||||||
// value = value + (((double)(*inp - '0'))/pow(10.0l,
|
|
||||||
// (double)(inp-oinp))); // Accumulate value.
|
|
||||||
value = value * 10 + (*inp - '0'); // Accumulate value.
|
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||||
dplaces++;
|
dplaces++;
|
||||||
inp++;
|
inp++;
|
||||||
}
|
}
|
||||||
value = value / pow(10, dplaces);
|
value = value / pow(10, dplaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
// > 1.20000
|
|
||||||
// = 1.0 + 2/10
|
|
||||||
|
|
||||||
// > 1.23
|
|
||||||
// = 1.2 + 3/100
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +57,7 @@ int yylex() {
|
|||||||
case '\n': return NL;
|
case '\n': return NL;
|
||||||
case '-': return NEG;
|
case '-': return NEG;
|
||||||
case '*': return MULT;
|
case '*': return MULT;
|
||||||
|
case '/': return DIV;
|
||||||
default: return CALL;
|
default: return CALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user