test
This commit is contained in:
72
lexer.c
72
lexer.c
@@ -9,18 +9,6 @@
|
|||||||
#define NB_IMPLEMENTATION
|
#define NB_IMPLEMENTATION
|
||||||
#include "nb.h"
|
#include "nb.h"
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
char* mstr;
|
|
||||||
} mstring;
|
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
int mint;
|
|
||||||
} mint;
|
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
float myfloat;
|
|
||||||
} mfloat;
|
|
||||||
|
|
||||||
int str_to_int(char *strint){
|
int str_to_int(char *strint){
|
||||||
int new_int = atoi(strint);
|
int new_int = atoi(strint);
|
||||||
return new_int;
|
return new_int;
|
||||||
@@ -43,7 +31,10 @@ typedef enum{
|
|||||||
TOKEN_DIV,
|
TOKEN_DIV,
|
||||||
TOKEN_UNKNOWN,
|
TOKEN_UNKNOWN,
|
||||||
TOKEN_EOF,
|
TOKEN_EOF,
|
||||||
TOKEN_NEWLINE
|
TOKEN_NEWLINE,
|
||||||
|
TOKEN_LPAREN,
|
||||||
|
TOKEN_RPAREN,
|
||||||
|
TOKEN_COMMA
|
||||||
} symbols;
|
} symbols;
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
@@ -91,6 +82,11 @@ struct ASTNode {
|
|||||||
ASTNode* left;
|
ASTNode* left;
|
||||||
ASTNode* right;
|
ASTNode* right;
|
||||||
} binary;
|
} binary;
|
||||||
|
struct {
|
||||||
|
char *name;
|
||||||
|
ASTNode** args;
|
||||||
|
size_t arg_count;
|
||||||
|
} func_call;
|
||||||
} data;
|
} data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -144,18 +140,26 @@ ASTNode* ast_new_binary(char op, ASTNode* l, ASTNode* r){
|
|||||||
}
|
}
|
||||||
|
|
||||||
ASTNode* parse_factor(parser* p) {
|
ASTNode* parse_factor(parser* p) {
|
||||||
Token tok = parser_peek(p);
|
Token tok = parser_peek(p);
|
||||||
if (tok.type == TOKEN_EOF) {
|
if (tok.type == TOKEN_EOF){
|
||||||
fprintf(stderr, "Unexpected end of input in factor\n");
|
fprintf(stderr, "Unexpected end of input in factor\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (tok.type == TOKEN_INTEGER || tok.type == TOKEN_FLOAT) {
|
|
||||||
parser_advance(p);
|
|
||||||
double v = atof(tok.text);
|
|
||||||
return ast_new_number(v);
|
|
||||||
}
|
|
||||||
fprintf(stderr, "Unexpected token '%s' in factor\n", tok.text);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (tok.type == TOKEN_INTEGER || tok.type == TOKEN_FLOAT){
|
||||||
|
parser_advance(p);
|
||||||
|
double v = atof(tok.text);
|
||||||
|
return ast_new_number(v);
|
||||||
|
}
|
||||||
|
// if (tok.type == TOKEN_STRING){
|
||||||
|
// parser_advance(p);
|
||||||
|
// char* func_name = tok.text;
|
||||||
|
// if (parser_match(p, TOKEN_LPAREN)){
|
||||||
|
// size_t argc_count = 0;
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
fprintf(stderr, "Unexpected token '%s' in factor\n", tok.text);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode* parse_term(parser* p) {
|
ASTNode* parse_term(parser* p) {
|
||||||
@@ -286,10 +290,26 @@ Token read_from_tok(char* text, uint cursor){
|
|||||||
mytoks.text = strdup("newline");
|
mytoks.text = strdup("newline");
|
||||||
mytoks.cursor_skip = 1;
|
mytoks.cursor_skip = 1;
|
||||||
break;
|
break;
|
||||||
|
case '(':
|
||||||
|
mytoks.type = TOKEN_LPAREN;
|
||||||
|
mytoks.text = strdup("(");
|
||||||
|
mytoks.behaviour = BHV_STACK;
|
||||||
|
break;
|
||||||
|
case ')':
|
||||||
|
mytoks.type = TOKEN_RPAREN;
|
||||||
|
mytoks.text = strdup(")");
|
||||||
|
mytoks.behaviour = BHV_STACK;
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
mytoks.type = TOKEN_COMMA;
|
||||||
|
mytoks.text = strdup(",");
|
||||||
|
mytoks.behaviour = BHV_STACK;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
mytoks.type = TOKEN_UNKNOWN;
|
mytoks.type = TOKEN_UNKNOWN;
|
||||||
mytoks.behaviour = BHV_UNDEFINED;
|
mytoks.behaviour = BHV_UNDEFINED;
|
||||||
mytoks.text = strdup(buf);
|
mytoks.text = strdup(buf);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -498,7 +518,7 @@ void mathparser(const char* input) {
|
|||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc > 1){
|
if (argc > 1){
|
||||||
char* input = nb_read_file(argv[1]);
|
char* input = nb_read_file(argv[1]);
|
||||||
printf("Input: %s\n", input);
|
// printf("Input: %s\n", input);
|
||||||
|
|
||||||
TokenArr toks = tokenize_all(input);
|
TokenArr toks = tokenize_all(input);
|
||||||
|
|
||||||
@@ -506,7 +526,7 @@ int main(int argc, char** argv) {
|
|||||||
ASTNode* root = parse_expression(&p);
|
ASTNode* root = parse_expression(&p);
|
||||||
|
|
||||||
double result = eval_ast(root);
|
double result = eval_ast(root);
|
||||||
printf("AST Result: %f\n", result);
|
printf("%f\n", result);
|
||||||
} else {
|
} else {
|
||||||
printf("Usage: %s <file>\n", argv[0]);
|
printf("Usage: %s <file>\n", argv[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user