Refactored error functionality and added Q-Expression functions to syntax
This commit is contained in:
parent
7824842704
commit
dd5de0cafa
9 changed files with 35 additions and 15 deletions
6
Makefile
6
Makefile
|
@ -1,5 +1,5 @@
|
||||||
run: prompt.c loperations.o lnumbers.o lexpressions.o lio.o mpc.o
|
run: prompt.c loperations.o lnumbers.o lexpressions.o lio.o lerror.o mpc.o
|
||||||
cc -std=c99 -Wall prompt.c loperations.o lnumbers.o lexpressions.o lio.o mpc.o -ledit -lm -o prompt
|
cc -std=c99 -Wall prompt.c loperations.o lnumbers.o lexpressions.o lio.o lerror.o mpc.o -ledit -lm -o prompt
|
||||||
loperations.o: lval/operations.c lval/operations.h
|
loperations.o: lval/operations.c lval/operations.h
|
||||||
cc -std=c99 -Wall -c lval/operations.c -o loperations.o
|
cc -std=c99 -Wall -c lval/operations.c -o loperations.o
|
||||||
lnumbers.o: lval/numbers.c lval/numbers.h
|
lnumbers.o: lval/numbers.c lval/numbers.h
|
||||||
|
@ -8,6 +8,8 @@ lexpressions.o: lval/expressions.c lval/expressions.h
|
||||||
cc -std=c99 -Wall -c lval/expressions.c -o lexpressions.o
|
cc -std=c99 -Wall -c lval/expressions.c -o lexpressions.o
|
||||||
lio.o: lval/io.c lval/io.h
|
lio.o: lval/io.c lval/io.h
|
||||||
cc -std=c99 -Wall -c lval/io.c -o lio.o
|
cc -std=c99 -Wall -c lval/io.c -o lio.o
|
||||||
|
lerror.o: lval/error.c lval/error.h
|
||||||
|
cc -std=c99 -Wall -c lval/error.c -o lerror.o
|
||||||
mpc.o: mpc.c mpc.h
|
mpc.o: mpc.c mpc.h
|
||||||
cc -std=c99 -Wall -lm -c mpc.c
|
cc -std=c99 -Wall -lm -c mpc.c
|
||||||
clean:
|
clean:
|
||||||
|
|
2
lval.h
2
lval.h
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
// Bring in the lval struct and lispy types
|
// Bring in the lval struct and lispy types
|
||||||
#include "lval/base.h"
|
#include "lval/base.h"
|
||||||
|
// Error functionality
|
||||||
|
#include "lval/error.h"
|
||||||
// Adds functionality for the numeric data type
|
// Adds functionality for the numeric data type
|
||||||
#include "lval/numbers.h"
|
#include "lval/numbers.h"
|
||||||
// Adds functionality for the expression (Q or S) data type
|
// Adds functionality for the expression (Q or S) data type
|
||||||
|
|
11
lval/error.c
Normal file
11
lval/error.c
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "error.h"
|
||||||
|
|
||||||
|
lval* lval_err(char* m) {
|
||||||
|
lval* v = (lval *) malloc(sizeof(lval));
|
||||||
|
v->type = LVAL_ERR;
|
||||||
|
v->err = (char *) malloc(strlen(m) + 1);
|
||||||
|
strcpy(v->err, m);
|
||||||
|
return v;
|
||||||
|
}
|
11
lval/error.h
Normal file
11
lval/error.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef LVAL_ERROR
|
||||||
|
#define LVAL_ERROR
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "base.h"
|
||||||
|
|
||||||
|
lval* lval_err(char* m);
|
||||||
|
|
||||||
|
#define LASSERT(args, cond, err) \
|
||||||
|
if (!(cond)) { lval_del(args); return lval_err(err); }
|
||||||
|
|
||||||
|
#endif
|
|
@ -2,8 +2,9 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "operations.h"
|
|
||||||
#include "expressions.h"
|
#include "expressions.h"
|
||||||
|
#include "operations.h"
|
||||||
|
#include "error.h"
|
||||||
|
|
||||||
// Think about where to put this declaration later
|
// Think about where to put this declaration later
|
||||||
lval* builtin_op(lval* v, char* op);
|
lval* builtin_op(lval* v, char* op);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
|
#include "error.h"
|
||||||
|
|
||||||
lval* lval_long(long x) {
|
lval* lval_long(long x) {
|
||||||
lval* v = (lval *) malloc(sizeof(lval));
|
lval* v = (lval *) malloc(sizeof(lval));
|
||||||
|
|
|
@ -12,14 +12,6 @@ lval* lval_sym(char* s) {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
lval* lval_err(char* m) {
|
|
||||||
lval* v = (lval *) malloc(sizeof(lval));
|
|
||||||
v->type = LVAL_ERR;
|
|
||||||
v->err = (char *) malloc(strlen(m) + 1);
|
|
||||||
strcpy(v->err, m);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
lval* lval_read(mpc_ast_t* t) {
|
lval* lval_read(mpc_ast_t* t) {
|
||||||
// If symbol or number, convert
|
// If symbol or number, convert
|
||||||
if (strstr(t->tag, "long")) { return lval_read_long(t); }
|
if (strstr(t->tag, "long")) { return lval_read_long(t); }
|
||||||
|
|
|
@ -4,9 +4,8 @@
|
||||||
#include "../mpc.h"
|
#include "../mpc.h"
|
||||||
#include "base.h"
|
#include "base.h"
|
||||||
|
|
||||||
// Constructor for other data types
|
// Constructor for symbol data type
|
||||||
lval* lval_sym(char* s);
|
lval* lval_sym(char* s);
|
||||||
lval* lval_err(char* m);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Methods to read (parse AST), evaluate,
|
Methods to read (parse AST), evaluate,
|
||||||
|
|
3
prompt.c
3
prompt.c
|
@ -54,7 +54,8 @@ int main (int argc, char** argv) {
|
||||||
"long : /-?[0-9]+/; "
|
"long : /-?[0-9]+/; "
|
||||||
"double : <long> '.' <number>; "
|
"double : <long> '.' <number>; "
|
||||||
"symbol : '+' | '-' | '*' | '/' | '%' \
|
"symbol : '+' | '-' | '*' | '/' | '%' \
|
||||||
| '^' | \"min\" | \"max\"; "
|
| '^' | \"min\" | \"max\" \
|
||||||
|
| \"list\" | \"head\" | \"tail\" | \"join\" | \"eval\" "
|
||||||
|
|
||||||
"sexpr : '(' <expr>* ')'; "
|
"sexpr : '(' <expr>* ')'; "
|
||||||
"qexpr : '{' <expr>* '}'; "
|
"qexpr : '{' <expr>* '}'; "
|
||||||
|
|
Reference in a new issue