Added the ability to call infix function backend.
This commit is contained in:
parent
bdca40bae4
commit
e05ebeef2a
@ -21,6 +21,10 @@
|
||||
|
||||
%define parse.error verbose
|
||||
|
||||
%token LGROUP
|
||||
%token RGROUP
|
||||
%token SEP
|
||||
|
||||
%token<strval> CALL
|
||||
%token<fval> NUM
|
||||
|
||||
@ -42,6 +46,14 @@ input:
|
||||
exp:
|
||||
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
||||
| NEG NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2)); }
|
||||
|
||||
| CALL LGROUP NUM SEP NUM RGROUP {
|
||||
AST** argv = calloc(2, sizeof(AST*));
|
||||
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($3));
|
||||
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($5));
|
||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, 2, argv));
|
||||
}
|
||||
|
||||
| NUM PLUS NUM {
|
||||
AST** argv = calloc(2, sizeof(AST*));
|
||||
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1));
|
||||
|
24
src/lexer.c
24
src/lexer.c
@ -3,6 +3,7 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "include/dstr.h"
|
||||
#include "include/lexer.h"
|
||||
|
||||
int acc_int(int c) {
|
||||
@ -37,6 +38,17 @@ double acc_float(int c) {
|
||||
return value;
|
||||
}
|
||||
|
||||
char* acc_word(int c) {
|
||||
Dstr* val = dstr_init();
|
||||
do {
|
||||
dstr_appendch(val, *(inp - 1));
|
||||
inp++;
|
||||
} while (isalpha(*inp));
|
||||
dstr_appendch(val, *(inp - 1));
|
||||
|
||||
return val->buf;
|
||||
}
|
||||
|
||||
int yylex() {
|
||||
if (*inp == '\0') return YYEOF;
|
||||
|
||||
@ -52,17 +64,23 @@ int yylex() {
|
||||
return NUM;
|
||||
}
|
||||
|
||||
if (isalpha(c)) {
|
||||
yylval.strval = acc_word(c);
|
||||
return CALL;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case '+': return PLUS;
|
||||
case '\n': return NL;
|
||||
case '-': return NEG;
|
||||
case '*': return MULT;
|
||||
case '/': return DIV;
|
||||
default: return CALL;
|
||||
case '(': return LGROUP;
|
||||
case ')': return RGROUP;
|
||||
case ',': return SEP;
|
||||
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unexpected character: %c\n", c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
void yyerror(char const* s) { fprintf(stderr, "Parse error: %s\n", s); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user