tweak: delete old code + make format

This commit is contained in:
NunoSempere 2023-05-01 18:33:52 -04:00
parent cf85977086
commit 5772490583
2 changed files with 188 additions and 271 deletions

BIN
mumble

Binary file not shown.

View File

@ -9,300 +9,215 @@
// Types // Types
typedef struct lispval { typedef struct lispval {
int type; int type;
long num; long num;
char* err; char* err;
char* sym; char* sym;
int count; int count;
struct lispval** cell; // list of lisval* struct lispval** cell; // list of lisval*
} lispval; } lispval;
enum { LISPVAL_NUM, LISPVAL_ERR, LISPVAL_SYM, LISPVAL_SEXPR }; enum {
enum { LISPERR_DIV_ZERO, LISPERR_BAD_OP, LISPERR_BAD_NUM }; LISPVAL_NUM,
LISPVAL_ERR,
LISPVAL_SYM,
LISPVAL_SEXPR
};
enum {
LISPERR_DIV_ZERO,
LISPERR_BAD_OP,
LISPERR_BAD_NUM
};
// Constructors // Constructors
lispval* lispval_num(long x){ lispval*
lispval* v = malloc(sizeof(lispval)); lispval_num(long x)
v->type = LISPVAL_NUM; {
v->num = x; lispval* v = malloc(sizeof(lispval));
return v; v->type = LISPVAL_NUM;
} v->num = x;
lispval* lispval_err(char* message){ return v;
lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_ERR;
v->err = malloc(strlen(message)+1);
strcpy(v->err, message);
return v;
}
lispval* lispval_sym(char* symbol){
lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_SYM;
v->sym = malloc(strlen(symbol)+1);
strcpy(v->sym, symbol);
return v;
}
lispval* lispval_sexpr(void){
lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_SEXPR;
v->count = 0;
v->cell = NULL;
return v;
} }
// Delete lispval*
void delete_lispval(lispval* v){ lispval_err(char* message)
switch(v->type){ {
case LISPVAL_NUM: break; lispval* v = malloc(sizeof(lispval));
case LISPVAL_ERR: free(v->err); break; v->type = LISPVAL_ERR;
case LISPVAL_SYM: free(v->sym); break; v->err = malloc(strlen(message) + 1);
case LISPVAL_SEXPR: strcpy(v->err, message);
for(int i=0; i< v->count; i++){ return v;
delete_lispval(v->cell[i]); }
}
free(v->cell); lispval*
break; lispval_sym(char* symbol)
} {
free(v); lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_SYM;
v->sym = malloc(strlen(symbol) + 1);
strcpy(v->sym, symbol);
return v;
}
lispval*
lispval_sexpr(void)
{
lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_SEXPR;
v->count = 0;
v->cell = NULL;
return v;
}
// Destructor
void delete_lispval(lispval* v)
{
switch (v->type) {
case LISPVAL_NUM:
break;
case LISPVAL_ERR:
free(v->err);
break;
case LISPVAL_SYM:
free(v->sym);
break;
case LISPVAL_SEXPR:
for (int i = 0; i < v->count; i++) {
delete_lispval(v->cell[i]);
}
free(v->cell);
break;
}
free(v);
} }
// Read ast into a lispval object // Read ast into a lispval object
lispval* lispval_append_child(lispval* parent, lispval* child){ lispval*
parent->count = parent->count + 1; lispval_append_child(lispval* parent, lispval* child)
parent->cell = realloc(parent->cell, sizeof(lispval) * parent->count); {
parent->cell[parent->count -1] = child; parent->count = parent->count + 1;
return parent; parent->cell = realloc(parent->cell, sizeof(lispval) * parent->count);
parent->cell[parent->count - 1] = child;
return parent;
} }
lispval* read_lispval_num(mpc_ast_t* t){ lispval*
errno = 0; read_lispval_num(mpc_ast_t* t)
long x = strtol(t->contents, NULL, 10); {
return errno != ERANGE ? lispval_num(x) : lispval_err("Error: Invalid number."); errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lispval_num(x)
: lispval_err("Error: Invalid number.");
} }
lispval* read_lispval(mpc_ast_t* t){ lispval*
if(strstr(t->tag, "number")){ read_lispval(mpc_ast_t* t)
return read_lispval_num(t); {
}else if(strstr(t->tag, "symbol")){ if (strstr(t->tag, "number")) {
return lispval_sym(t->contents); return read_lispval_num(t);
} else if((strcmp(t->tag, ">") == 0) || strstr(t->tag, "sexpr")){ } else if (strstr(t->tag, "symbol")) {
lispval* x = lispval_sexpr(); return lispval_sym(t->contents);
for(int i=0; i<(t->children_num); i++){ } else if ((strcmp(t->tag, ">") == 0) || strstr(t->tag, "sexpr")) {
if (strcmp(t->children[i]->contents, "(") == 0) { continue; } lispval* x = lispval_sexpr();
if (strcmp(t->children[i]->contents, ")") == 0) { continue; } for (int i = 0; i < (t->children_num); i++) {
if (strcmp(t->children[i]->tag, "regex") == 0) { continue; } if (strcmp(t->children[i]->contents, "(") == 0) {
x = lispval_append_child(x, read_lispval(t->children[i])); continue;
} }
return x; if (strcmp(t->children[i]->contents, ")") == 0) {
}else{ continue;
lispval* err = lispval_err("Unknown ast type."); }
return err; if (strcmp(t->children[i]->tag, "regex") == 0) {
} continue;
}
x = lispval_append_child(x, read_lispval(t->children[i]));
}
return x;
} else {
lispval* err = lispval_err("Unknown ast type.");
return err;
}
} }
// Print // Print
void print_lispval_tree(lispval* v, int indent_level) void print_lispval_tree(lispval* v, int indent_level)
{ {
char* indent = malloc(sizeof(char)*(indent_level+1)); // ""; char* indent = malloc(sizeof(char) * (indent_level + 1)); // "";
for(int i=0; i<indent_level;i++){ for (int i = 0; i < indent_level; i++) {
indent[i] = ' '; indent[i] = ' ';
} }
indent[indent_level] = '\0'; indent[indent_level] = '\0';
switch(v->type){ switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
printf("\n%sNumber: %li", indent, v->num); printf("\n%sNumber: %li", indent, v->num);
break; break;
case LISPVAL_ERR: case LISPVAL_ERR:
printf("\n%sError: %s", indent, v->err); printf("\n%sError: %s", indent, v->err);
break; break;
case LISPVAL_SYM: case LISPVAL_SYM:
printf("\n%sSymbol: %s", indent, v->sym); printf("\n%sSymbol: %s", indent, v->sym);
break; break;
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
printf("\n%sSExpr, with %d children:", indent, v->count); printf("\n%sSExpr, with %d children:", indent, v->count);
for(int i=0; i<v->count; i++){ for (int i = 0; i < v->count; i++) {
print_lispval_tree(v->cell[i], indent_level + 2); print_lispval_tree(v->cell[i], indent_level + 2);
} }
break; break;
default: default:
printf("Error: unknown lispval type\n"); printf("Error: unknown lispval type\n");
printf("%s", v->sym); printf("%s", v->sym);
} }
free(indent); free(indent);
} }
void print_lispval_parenthesis(lispval* v) void print_lispval_parenthesis(lispval* v)
{ {
switch(v->type){ switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
printf("%li ", v->num); printf("%li ", v->num);
break; break;
case LISPVAL_ERR: case LISPVAL_ERR:
printf("[Error: %s] ", v->err); printf("[Error: %s] ", v->err);
break; break;
case LISPVAL_SYM: case LISPVAL_SYM:
printf("%s ", v->sym); printf("%s ", v->sym);
break; break;
case LISPVAL_SEXPR: case LISPVAL_SEXPR:
printf("( "); printf("( ");
for(int i=0; i<v->count; i++){ for (int i = 0; i < v->count; i++) {
print_lispval_parenthesis(v->cell[i]); print_lispval_parenthesis(v->cell[i]);
} }
printf(") "); printf(") ");
break; break;
default: default:
printf("Error: unknown lispval type\n"); printf("Error: unknown lispval type\n");
printf("%s", v->sym); printf("%s", v->sym);
} }
}
/*
void print_lispval(lispval l){
switch(l.type){
case LISPVAL_NUM:
printf("\n%li", l.num);
break;
case LISPVAL_ERR:
switch(l.err){
case LISPERR_BAD_OP:
printf("\nError: Invalid operator");
break;
case LISPERR_BAD_NUM:
printf("\nError: Invalid number");
break;
case LISPERR_DIV_ZERO:
printf("\nError: Division by zero");
break;
default:
printf("\nError: Unknown error");
}
break;
default:
printf("\nUnknown lispval type");
}
}
*/
// Utils
int is_ignorable(mpc_ast_t* t){
int is_regex = !strcmp(t->tag, "regex");
int is_parenthesis = !strcmp(t->tag, "char") && !strcmp(t->contents, "(");
return is_regex || is_parenthesis;
} }
void print_ast(mpc_ast_t* ast, int indent_level) void print_ast(mpc_ast_t* ast, int indent_level)
{ {
char* indent = malloc(sizeof(char)*(indent_level+1)); // ""; char* indent = malloc(sizeof(char) * (indent_level + 1)); // "";
for(int i=0; i<indent_level;i++){ for (int i = 0; i < indent_level; i++) {
indent[i] = ' '; indent[i] = ' ';
} }
indent[indent_level] = '\0'; indent[indent_level] = '\0';
printf("\n%sTag: %s", indent, ast->tag); printf("\n%sTag: %s", indent, ast->tag);
printf("\n%sContents: %s", indent, strcmp(ast->contents, "") ? ast->contents : "None"); printf("\n%sContents: %s", indent,
strcmp(ast->contents, "") ? ast->contents : "None");
printf("\n%sNumber of children: %i", indent, ast->children_num); printf("\n%sNumber of children: %i", indent, ast->children_num);
/* Print the children */ /* Print the children */
for (int i = 0; i < ast->children_num; i++) { for (int i = 0; i < ast->children_num; i++) {
mpc_ast_t* child_i = ast->children[i]; mpc_ast_t* child_i = ast->children[i];
printf("\n%sChild #%d", indent, i); printf("\n%sChild #%d", indent, i);
print_ast(child_i, indent_level + 2); print_ast(child_i, indent_level + 2);
} }
free(indent); free(indent);
} }
// Operations
/*
lispval evaluate_unary_operation(char* op, lispval x)
{
if(x.type == LISPVAL_ERR) return x;
if (!strcmp(op, "+")) {
return lispval_num(x.num);
} else if (!strcmp(op, "-")) {
return lispval_num(-x.num);
}
return lispval_err(LISPERR_BAD_OP);
}
lispval evaluate_operation(char* op, lispval x, lispval y)
{
if(x.type == LISPVAL_ERR) return x;
if(y.type == LISPVAL_ERR) return y;
if (!strcmp(op, "+")) {
return lispval_num(x.num + y.num);
} else if (!strcmp(op, "-")) {
return lispval_num(x.num - y.num);
} else if (!strcmp(op, "*")) {
return lispval_num(x.num * y.num);
} else if (!strcmp(op, "/")) {
if (y.num == 0) return lispval_err(LISPERR_DIV_ZERO);
return lispval_num(x.num / y.num);
}
return lispval_err(LISPERR_BAD_OP);
}
// Evaluate the AST
lispval evaluate_ast(mpc_ast_t* t)
{
// Base case #1: It's a number
if (strstr(t->tag, "number")) {
if (VERBOSE)
printf("\nCase #1, %s", t->contents);
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lispval_num(x) : lispval_err(LISPERR_BAD_NUM);
}
// Base case #2: It's a number inputted into the REPL
// note: strcmp returns a 0 if both chars* have the same contents.
if (t->children_num == 2 && strstr(t->children[0]->tag, "number") && !strcmp(t->children[1]->tag, "regex")) {
if (VERBOSE)
printf("\nCase #2, top level num");
return evaluate_ast(t->children[0]);
}
// Base case #3: Top level parenthesis
if (t->children_num == 2 && strstr(t->children[0]->tag, "expr|>") && !strcmp(t->children[1]->tag, "regex")) {
if (VERBOSE)
printf("\nCase #3, top level parenthesis");
return evaluate_ast(t->children[0]);
}
// "Real" cases
lispval x;
char* operation;
// Case #4: Top level unary operation
if (t->children_num == 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) {
operation = t->children[1]->contents;
if (VERBOSE)
printf("\nCase #4, unary operation %s", operation);
x = evaluate_ast(t->children[2]);
x = evaluate_unary_operation(operation, x);
}
// Case #5: Binary (or more) operations case
if (t->children_num > 3 && is_ignorable(t->children[0]) && strstr(t->children[1]->tag, "operator")) {
operation = t->children[1]->contents;
if (VERBOSE)
printf("\nCase #5, %s", operation);
x = evaluate_ast(t->children[2]);
int i = 3;
int is_unary = 1;
while ((i < t->children_num) && strstr(t->children[i]->tag, "expr")) {
// note that when reaching a closing parenthesis, ^ returns false
lispval y = evaluate_ast(t->children[i]);
x = evaluate_operation(operation, x, y);
i++;
is_unary = 0;
}
if(is_unary){
printf("\nCase #5.b, unary operation %s", operation);
x = evaluate_unary_operation(operation, x);
}
}
return x;
}
*/
// Main // Main
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// Info // Info
puts("Mumble version 0.0.2\n"); puts("Mumble version 0.0.2\n");
puts("Press Ctrl+C/Ctrl+D to exit\n"); puts("Press Ctrl+C/Ctrl+D to exit\n");
@ -314,8 +229,7 @@ int main(int argc, char** argv)
mpc_parser_t* Mumble = mpc_new("mumble"); mpc_parser_t* Mumble = mpc_new("mumble");
/* Define them with the following Language */ /* Define them with the following Language */
mpca_lang(MPCA_LANG_DEFAULT, mpca_lang(MPCA_LANG_DEFAULT, " \
" \
number : /-?[0-9]+/ ; \ number : /-?[0-9]+/ ; \
symbol : '+' | '-' | '*' | '/' ; \ symbol : '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \ sexpr : '(' <expr>* ')' ; \
@ -323,9 +237,8 @@ int main(int argc, char** argv)
mumble : /^/ <expr>* /$/ ; \ mumble : /^/ <expr>* /$/ ; \
", ",
Number, Symbol, Sexpr, Expr, Mumble); Number, Symbol, Sexpr, Expr, Mumble);
// Initialize a repl // Initialize a repl
int loop = 1; int loop = 1;
while (loop) { while (loop) {
char* input = readline("mumble> "); char* input = readline("mumble> ");
@ -342,16 +255,20 @@ int main(int argc, char** argv)
mpc_ast_t* ast = result.output; mpc_ast_t* ast = result.output;
// Print AST if VERBOSE // Print AST if VERBOSE
if(VERBOSE) print_ast(ast, 0); if (VERBOSE)
print_ast(ast, 0);
// Evaluate the AST
// if(VERBOSE) printf("\n\nEvaluating the AST");
// lispval result = evaluate_ast(ast);
lispval* l = read_lispval(ast);
if(VERBOSE) printf("\n\nTree printing: "); print_lispval_tree(l, 0);
if(VERBOSE) printf("\nParenthesis printing: \n"); print_lispval_parenthesis(l);
delete_lispval(l);
// Evaluate the AST
// if(VERBOSE) printf("\n\nEvaluating the AST");
// lispval result = evaluate_ast(ast);
lispval* l = read_lispval(ast);
if (VERBOSE)
printf("\n\nTree printing: ");
print_lispval_tree(l, 0);
if (VERBOSE)
printf("\nParenthesis printing: \n");
print_lispval_parenthesis(l);
delete_lispval(l);
} else { } else {
/* Otherwise Print the Error */ /* Otherwise Print the Error */
mpc_err_print(result.error); mpc_err_print(result.error);