Archived
1
0
Fork 0

Refactored error functionality and added Q-Expression functions to syntax

This commit is contained in:
Brandon Rozek 2018-06-07 19:36:52 -04:00
parent 7824842704
commit dd5de0cafa
9 changed files with 35 additions and 15 deletions

View file

@ -1,5 +1,5 @@
run: prompt.c loperations.o lnumbers.o lexpressions.o lio.o mpc.o
cc -std=c99 -Wall prompt.c loperations.o lnumbers.o lexpressions.o lio.o mpc.o -ledit -lm -o prompt
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 lerror.o mpc.o -ledit -lm -o prompt
loperations.o: lval/operations.c lval/operations.h
cc -std=c99 -Wall -c lval/operations.c -o loperations.o
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
lio.o: lval/io.c lval/io.h
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
cc -std=c99 -Wall -lm -c mpc.c
clean:

2
lval.h
View file

@ -4,6 +4,8 @@
// Bring in the lval struct and lispy types
#include "lval/base.h"
// Error functionality
#include "lval/error.h"
// Adds functionality for the numeric data type
#include "lval/numbers.h"
// Adds functionality for the expression (Q or S) data type

11
lval/error.c Normal file
View 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
View 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

View file

@ -2,8 +2,9 @@
#include <string.h>
#include <stdlib.h>
#include "operations.h"
#include "expressions.h"
#include "operations.h"
#include "error.h"
// Think about where to put this declaration later
lval* builtin_op(lval* v, char* op);

View file

@ -1,5 +1,6 @@
#include <stdlib.h>
#include "numbers.h"
#include "error.h"
lval* lval_long(long x) {
lval* v = (lval *) malloc(sizeof(lval));

View file

@ -12,14 +12,6 @@ lval* lval_sym(char* s) {
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) {
// If symbol or number, convert
if (strstr(t->tag, "long")) { return lval_read_long(t); }

View file

@ -4,9 +4,8 @@
#include "../mpc.h"
#include "base.h"
// Constructor for other data types
// Constructor for symbol data type
lval* lval_sym(char* s);
lval* lval_err(char* m);
/*
Methods to read (parse AST), evaluate,

View file

@ -53,8 +53,9 @@ int main (int argc, char** argv) {
"number : /[0-9]+/; "
"long : /-?[0-9]+/; "
"double : <long> '.' <number>; "
"symbol : '+' | '-' | '*' | '/' | '%' \
| '^' | \"min\" | \"max\"; "
"symbol : '+' | '-' | '*' | '/' | '%' \
| '^' | \"min\" | \"max\" \
| \"list\" | \"head\" | \"tail\" | \"join\" | \"eval\" "
"sexpr : '(' <expr>* ')'; "
"qexpr : '{' <expr>* '}'; "