test
This commit is contained in:
50
lexer.c
50
lexer.c
@@ -9,18 +9,6 @@
|
||||
#define NB_IMPLEMENTATION
|
||||
#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 new_int = atoi(strint);
|
||||
return new_int;
|
||||
@@ -43,7 +31,10 @@ typedef enum{
|
||||
TOKEN_DIV,
|
||||
TOKEN_UNKNOWN,
|
||||
TOKEN_EOF,
|
||||
TOKEN_NEWLINE
|
||||
TOKEN_NEWLINE,
|
||||
TOKEN_LPAREN,
|
||||
TOKEN_RPAREN,
|
||||
TOKEN_COMMA
|
||||
} symbols;
|
||||
|
||||
typedef enum{
|
||||
@@ -91,6 +82,11 @@ struct ASTNode {
|
||||
ASTNode* left;
|
||||
ASTNode* right;
|
||||
} binary;
|
||||
struct {
|
||||
char *name;
|
||||
ASTNode** args;
|
||||
size_t arg_count;
|
||||
} func_call;
|
||||
} data;
|
||||
};
|
||||
|
||||
@@ -154,6 +150,14 @@ ASTNode* parse_factor(parser* 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);
|
||||
}
|
||||
@@ -286,11 +290,27 @@ Token read_from_tok(char* text, uint cursor){
|
||||
mytoks.text = strdup("newline");
|
||||
mytoks.cursor_skip = 1;
|
||||
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:
|
||||
mytoks.type = TOKEN_UNKNOWN;
|
||||
mytoks.behaviour = BHV_UNDEFINED;
|
||||
mytoks.text = strdup(buf);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
return mytoks;
|
||||
@@ -498,7 +518,7 @@ void mathparser(const char* input) {
|
||||
int main(int argc, char** argv) {
|
||||
if (argc > 1){
|
||||
char* input = nb_read_file(argv[1]);
|
||||
printf("Input: %s\n", input);
|
||||
// printf("Input: %s\n", input);
|
||||
|
||||
TokenArr toks = tokenize_all(input);
|
||||
|
||||
@@ -506,7 +526,7 @@ int main(int argc, char** argv) {
|
||||
ASTNode* root = parse_expression(&p);
|
||||
|
||||
double result = eval_ast(root);
|
||||
printf("AST Result: %f\n", result);
|
||||
printf("%f\n", result);
|
||||
} else {
|
||||
printf("Usage: %s <file>\n", argv[0]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user