Add "if" built-in
diff --git a/src/lisp/.clang-format b/src/lisp/.clang-format
index 419e428..45b774a 100644
--- a/src/lisp/.clang-format
+++ b/src/lisp/.clang-format
@@ -1,12 +1,4 @@
+---
BasedOnStyle: Microsoft
-
-AlignAfterOpenBracket: Align
-SpacesInSquareBrackets: true
-SpaceInEmptyBlock: true
-SpacesInConditionalStatement: true
-SpaceBeforeParens: Always
UseTab: ForIndentation
-TabWidth: 4
ColumnLimit: 80
-
-ForEachMacros: ['FOREACH']
\ No newline at end of file
diff --git a/src/lisp/Jmk b/src/lisp/Jmk
index 17ee1ff..aa75bf6 100644
--- a/src/lisp/Jmk
+++ b/src/lisp/Jmk
@@ -16,13 +16,8 @@
OBJECTS = main.o \
lisp.o \
compiler.o \
- lib/std.o
-
-ifeq ($(PLAT),linux)
-OBJECTS += plat/linux.o
-else
-$(error linux is the only supported option for PLAT)
-endif
+ lib/std.o \
+ plat/linux.o
LUA = vendor/luajit/src/host/minilua
@@ -42,6 +37,6 @@
format:
status_log(FORMAT, *)
- @clang-format -i *.c *.h *.dasc
+ @clang-format -i *.c *.h *.dasc plat/* lib/*
finish
diff --git a/src/lisp/compiler.dasc b/src/lisp/compiler.dasc
index eac885a..e5a9444 100644
--- a/src/lisp/compiler.dasc
+++ b/src/lisp/compiler.dasc
@@ -1,13 +1,13 @@
/* -*- mode:c -*- */
#include "compiler.h"
-#include "plat/plat.h"
#include "lib/std.h"
+#include "plat/plat.h"
#include <dasm_proto.h>
#include <dasm_x86.h>
-#define value_size sizeof (value_t)
+#define value_size sizeof(value_t)
|.arch x86;
@@ -26,13 +26,13 @@
dasm_State *d;
unsigned int npc = 8;
-struct function *find_function (struct environment *env, char *name)
+struct function *find_function(struct environment *env, char *name)
{
struct function *f = env->first;
- while ( strcmp (f->name, name) != 0 )
+ while (strcmp(f->name, name) != 0)
{
- if ( f->prev )
+ if (f->prev)
f = f->prev;
else
return NULL;
@@ -41,120 +41,160 @@
return f;
}
-void compile_tl (value_t val, struct environment *env)
+void compile_tl(value_t val, struct environment *env)
{
- if ( !listp (val) )
- err ("Top level must be a list");
-
- value_t form = car (val);
- value_t args = cdr (val);
+ if (!listp(val))
+ err("Top level must be a list");
- if ( symstreq (form, "defun") )
+ value_t form = car(val);
+ value_t args = cdr(val);
+
+ if (symstreq(form, "defun"))
{
dasm_State *d;
dasm_State **Dst = &d;
|.section code;
- dasm_init (&d, DASM_MAXSECTION);
-
+ dasm_init(&d, DASM_MAXSECTION);
+
|.globals lbl_;
- void *labels[ lbl__MAX ];
- dasm_setupglobal (&d, labels, lbl__MAX);
-
+ void *labels[lbl__MAX];
+ dasm_setupglobal(&d, labels, lbl__MAX);
+
|.actionlist lisp_actions;
- dasm_setup (&d, lisp_actions);
-
- dasm_growpc (&d, npc);
+ dasm_setup(&d, lisp_actions);
+
+ dasm_growpc(&d, npc);
struct local local;
local.first = NULL;
local.num_vars = 0;
-
+
// Generate code
-
+
| setup 0;
-
- value_t name = car (args);
- args = cdr (args);
- value_t arglist = car (args);
- value_t body = cdr (args);
- if ( (name & HEAP_MASK) != SYMBOL_TAG )
- err ("function name must be a symbol");
+ value_t name = car(args);
+ args = cdr(args);
+ value_t arglist = car(args);
+ value_t body = cdr(args);
- for ( ; !nilp (body); body = cdr (body) )
+ if ((name & HEAP_MASK) != SYMBOL_TAG)
+ err("function name must be a symbol");
+
+ for (; !nilp(body); body = cdr(body))
{
- compile_expression (env, &local, car (body), Dst);
+ compile_expression(env, &local, car(body), Dst);
}
| cleanup;
- add_function (env, (char *) (name ^ SYMBOL_TAG), link (Dst), length (arglist));
+ add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
+ length(arglist));
- dasm_free (&d);
+ dasm_free(&d);
}
}
-struct environment compile_all (struct istream *is)
+struct environment compile_all(struct istream *is)
{
value_t val;
struct environment env;
env.first = NULL;
- load_std (&env);
-
- while ( read1 (is, &val) )
+ load_std(&env);
+
+ while (read1(is, &val))
{
- compile_tl (val, &env);
+ compile_tl(val, &env);
}
return env;
}
-void compile_expression (struct environment *env, struct local *local,
- value_t val, dasm_State **Dst)
+int nextpc(struct local *local, dasm_State **Dst)
{
- if ( integerp (val) || stringp (val) || symbolp (val) )
+ int n = local->nextpc++;
+ if (n > local->npc)
+ {
+ local->npc += 16;
+ dasm_growpc(Dst, local->npc);
+ }
+ return n;
+}
+
+void compile_expression(struct environment *env, struct local *local,
+ value_t val, dasm_State **Dst)
+{
+ if (symstreq(val, "nil"))
+ {
+ | mov eax, (nil);
+ }
+ else if (integerp(val) || stringp(val) || symbolp(val))
{
| mov eax, val;
}
- else if ( listp (val) )
+ else if (listp(val))
{
- value_t fsym = car (val);
+ value_t fsym = car(val);
+ value_t args = cdr(val);
+ int nargs = length(args);
- if ( !symbolp (fsym) )
+ if (!symbolp(fsym))
{
- err ("function name must be a symbol");
+ err("function name must be a symbol");
}
- struct function *func = find_function (env, (char *) (fsym ^ SYMBOL_TAG));
- value_t args = cdr (val);
- int nargs = length (args);
-
- if ( nargs != func->nargs )
- err ("wrong number of args");
-
- for ( int i = length (args) - 1; i >= 0; i-- )
+ if (symstreq(fsym, "if"))
{
- compile_expression (env, local, elt (args, i), Dst);
- | push eax;
- }
+ if (nargs < 2 || nargs > 3)
+ err("Must give at least 2 arguments to if");
- | mov ebx, (func->code_addr);
- | call ebx;
- | add esp, (nargs * 4);
- // result in eax
+ compile_expression(env, local, car(args), Dst);
+ int false_label = nextpc(local, Dst),
+ after_label = nextpc(local, Dst);
+
+ // result is in eax
+ | cmp eax, (nil);
+ | je =>false_label;
+
+ compile_expression(env, local, elt(args, 1), Dst);
+
+ |=>false_label:
+ if (nargs == 3)
+ compile_expression(env, local, elt(args, 2), Dst);
+ |=>after_label:
+ }
+ else
+ {
+ struct function *func =
+ find_function(env, (char *)(fsym ^ SYMBOL_TAG));
+
+ if (nargs != func->nargs)
+ err("wrong number of args");
+
+ for (int i = length(args) - 1; i >= 0; i--)
+ {
+ compile_expression(env, local, elt(args, i), Dst);
+ | push eax;
+ }
+
+ | mov ebx, (func->code_addr);
+ | call ebx;
+ | add esp, (nargs * 4);
+ // result in eax
+ }
}
}
-void compile_expr_to_func (struct environment *env, char *name, value_t val,
- dasm_State **Dst)
+void compile_expr_to_func(struct environment *env, char *name, value_t val,
+ dasm_State **Dst)
{
| setup 0;
struct local local;
- compile_expression (env, &local, val, Dst);
-
+ compile_expression(env, &local, val, Dst);
+
| cleanup;
- add_function (env, name, link (Dst), 0);
+ add_function(env, name, link(Dst), 0);
}
diff --git a/src/lisp/compiler.h b/src/lisp/compiler.h
index b3bd89c..ae66001 100644
--- a/src/lisp/compiler.h
+++ b/src/lisp/compiler.h
@@ -11,10 +11,10 @@
int nargs; // number of arguments
union {
- value_t (*def0) ();
- value_t (*def1) (value_t);
- value_t (*def2) (value_t, value_t);
- value_t (*def3) (value_t, value_t, value_t);
+ value_t (*def0)();
+ value_t (*def1)(value_t);
+ value_t (*def2)(value_t, value_t);
+ value_t (*def3)(value_t, value_t, value_t);
void *code_ptr;
uintptr_t code_addr;
};
@@ -39,13 +39,16 @@
{
int num_vars;
struct variable *first;
+ int npc;
+ int nextpc;
};
-void compile_expression (struct environment *env, struct local *local,
- value_t val, dasm_State **Dst);
-void compile_expr_to_func (struct environment *env, char *name, value_t val,
- dasm_State **Dst);
+void compile_expression(struct environment *env, struct local *local,
+ value_t val, dasm_State **Dst);
+void compile_expr_to_func(struct environment *env, char *name, value_t val,
+ dasm_State **Dst);
+int nextpc(struct local *local, dasm_State **Dst);
// Compile top-level declaration
-void compile_tl (value_t val, struct environment *env);
-struct environment compile_all (struct istream *is);
-struct function *find_function (struct environment *env, char *name);
+void compile_tl(value_t val, struct environment *env);
+struct environment compile_all(struct istream *is);
+struct function *find_function(struct environment *env, char *name);
diff --git a/src/lisp/lib/std.c b/src/lisp/lib/std.c
index 9c1f3cb..03e17fc 100644
--- a/src/lisp/lib/std.c
+++ b/src/lisp/lib/std.c
@@ -1,48 +1,47 @@
#include "std.h"
#include <stdlib.h>
-value_t l_plus (value_t a, value_t b)
+value_t l_plus(value_t a, value_t b)
{
- if ( !integerp (a) || !integerp (b) )
+ if (!integerp(a) || !integerp(b))
return nil;
return (((a >> 2) + (b >> 2)) << 2) | INT_TAG;
}
-value_t l_minus (value_t a, value_t b)
+value_t l_minus(value_t a, value_t b)
{
- if ( !integerp (a) || !integerp (b) )
+ if (!integerp(a) || !integerp(b))
return nil;
return (((a >> 2) - (b >> 2)) << 2) | INT_TAG;
}
-value_t l_times (value_t a, value_t b)
+value_t l_times(value_t a, value_t b)
{
- if ( !integerp (a) || !integerp (b) )
+ if (!integerp(a) || !integerp(b))
return nil;
return (((a >> 2) * (b >> 2)) << 2) | INT_TAG;
}
-value_t l_divide (value_t a, value_t b)
+value_t l_divide(value_t a, value_t b)
{
- if ( !integerp (a) || !integerp (b) )
+ if (!integerp(a) || !integerp(b))
return nil;
return (((a >> 2) / (b >> 2)) << 2) | INT_TAG;
}
-value_t l_printval (value_t val)
+value_t l_printval(value_t val)
{
- printval (val, 0);
+ printval(val, 0);
return nil;
}
-void add_function (struct environment *env, char *name, void *func, int nargs)
+void add_function(struct environment *env, char *name, void *func, int nargs)
{
- struct function *last,
- *new = malloc (sizeof (struct function));
+ struct function *last, *new = malloc(sizeof(struct function));
last = env->first;
new->prev = last;
@@ -53,16 +52,16 @@
env->first = new;
}
-void load_std (struct environment *env)
+void load_std(struct environment *env)
{
- add_function (env, "+", l_plus, 2);
- add_function (env, "-", l_minus, 2);
- add_function (env, "*", l_times, 2);
- add_function (env, "/", l_divide, 2);
+ add_function(env, "+", l_plus, 2);
+ add_function(env, "-", l_minus, 2);
+ add_function(env, "*", l_times, 2);
+ add_function(env, "/", l_divide, 2);
- add_function (env, "car", car, 1);
- add_function (env, "cdr", cdr, 1);
- add_function (env, "cons", cons, 2);
+ add_function(env, "car", car, 1);
+ add_function(env, "cdr", cdr, 1);
+ add_function(env, "cons", cons, 2);
- add_function (env, "print", l_printval, 1);
+ add_function(env, "print", l_printval, 1);
}
diff --git a/src/lisp/lib/std.h b/src/lisp/lib/std.h
index 85d766c..000129e 100644
--- a/src/lisp/lib/std.h
+++ b/src/lisp/lib/std.h
@@ -1,9 +1,9 @@
#pragma once
-#include "../lisp.h"
#include "../compiler.h"
+#include "../lisp.h"
-value_t l_plus (value_t a, value_t b);
+value_t l_plus(value_t a, value_t b);
-void add_function (struct environment *env, char *name, void *func, int nargs);
-void load_std (struct environment *env);
+void add_function(struct environment *env, char *name, void *func, int nargs);
+void load_std(struct environment *env);
diff --git a/src/lisp/lisp b/src/lisp/lisp
index 807bf9f..ecb2972 100755
--- a/src/lisp/lisp
+++ b/src/lisp/lisp
Binary files differ
diff --git a/src/lisp/lisp.c b/src/lisp/lisp.c
index e2b02be..7a7d6cb 100644
--- a/src/lisp/lisp.c
+++ b/src/lisp/lisp.c
@@ -13,31 +13,31 @@
value_t nil = 0b00101111; // magic ;)
-void err (const char *msg)
+void err(const char *msg)
{
- fprintf (stderr, "ERROR: %s\n", msg);
- exit (1);
+ fprintf(stderr, "ERROR: %s\n", msg);
+ exit(1);
}
-value_t intval (int i)
+value_t intval(int i)
{
i <<= 2;
i |= INT_TAG;
return i;
}
-value_t cons (value_t car, value_t cdr)
+value_t cons(value_t car, value_t cdr)
{
- struct cons *c = malloc_aligned (sizeof (struct cons));
+ struct cons *c = malloc_aligned(sizeof(struct cons));
c->car = car;
c->cdr = cdr;
- struct alloc_list *item = malloc (sizeof (struct alloc_list));
+ struct alloc_list *item = malloc(sizeof(struct alloc_list));
item->type = T_CONS;
item->cons_val = c;
- if ( last_a )
+ if (last_a)
{
item->prev = last_a;
last_a->next = item;
@@ -55,55 +55,55 @@
return v;
}
-void skipws (struct istream *is)
+void skipws(struct istream *is)
{
- while ( isspace (is->peek (is)) )
- is->get (is);
+ while (isspace(is->peek(is)))
+ is->get(is);
}
-bool isallowedchar (char c)
+bool isallowedchar(char c)
{
return (c >= '#' && c <= '\'') || (c >= '*' && c <= '/') ||
(c >= '>' && c <= '@');
}
-bool issymstart (char c)
+bool issymstart(char c)
{
- return isalpha (c) || isallowedchar (c);
+ return isalpha(c) || isallowedchar(c);
}
-bool issym (char c)
+bool issym(char c)
{
- return isalpha (c) || isallowedchar (c) || isdigit (c);
+ return isalpha(c) || isallowedchar(c) || isdigit(c);
}
-bool readsym (struct istream *is, value_t *val)
+bool readsym(struct istream *is, value_t *val)
{
- skipws (is);
+ skipws(is);
- if ( !issymstart (is->peek (is)) )
+ if (!issymstart(is->peek(is)))
return false;
int size = 8;
- char *s = malloc_aligned (size);
+ char *s = malloc_aligned(size);
- s[ 0 ] = is->get (is);
+ s[0] = is->get(is);
- for ( int i = 1;; i++ )
+ for (int i = 1;; i++)
{
- if ( issym (is->peek (is)) )
+ if (issym(is->peek(is)))
{
- if ( i >= size )
+ if (i >= size)
{
size *= 2;
- s = realloc_aligned (s, size);
+ s = realloc_aligned(s, size);
}
- s[ i ] = is->get (is);
+ s[i] = is->get(is);
}
else
{
- s[ i ] = 0;
+ s[i] = 0;
*val = (value_t)s;
*val |= SYMBOL_TAG;
@@ -112,37 +112,37 @@
}
}
-bool readstr (struct istream *is, value_t *val)
+bool readstr(struct istream *is, value_t *val)
{
- skipws (is);
+ skipws(is);
- if ( is->peek (is) != '"' )
+ if (is->peek(is) != '"')
return false;
bool escape = false;
int size = 8;
- char *s = malloc_aligned (size);
+ char *s = malloc_aligned(size);
- (void)is->get (is);
+ (void)is->get(is);
- for ( int i = 0;; i++ )
+ for (int i = 0;; i++)
{
- if ( is->peek (is) != '"' )
+ if (is->peek(is) != '"')
{
- if ( i >= size )
+ if (i >= size)
{
size *= 2;
- s = realloc_aligned (s, size);
+ s = realloc_aligned(s, size);
}
- char c = is->get (is);
+ char c = is->get(is);
- if ( escape && c == 'n' )
+ if (escape && c == 'n')
c = '\n';
- else if ( escape && c == '\\' )
+ else if (escape && c == '\\')
c = '\\';
- if ( c == '\\' && !escape )
+ if (c == '\\' && !escape)
{
escape = true;
i--; // will be incremented again, UGLY.
@@ -150,12 +150,12 @@
else
{
escape = false;
- s[ i ] = c;
+ s[i] = c;
}
}
else
{
- is->get (is);
+ is->get(is);
*val = (value_t)s;
*val |= STRING_TAG;
@@ -165,118 +165,118 @@
}
}
-void printval (value_t v, int depth)
+void printval(value_t v, int depth)
{
- for ( int i = 0; i < depth; i++ )
- printf (" ");
+ for (int i = 0; i < depth; i++)
+ printf(" ");
- if ( symbolp (v) )
+ if (symbolp(v))
{
- printf ("'%s\n", (char *)(v ^ SYMBOL_TAG));
+ printf("'%s\n", (char *)(v ^ SYMBOL_TAG));
}
- else if ( stringp (v) )
+ else if (stringp(v))
{
- printf ("\"%s\"\n", (char *)(v ^ STRING_TAG));
+ printf("\"%s\"\n", (char *)(v ^ STRING_TAG));
}
- else if ( integerp (v) )
+ else if (integerp(v))
{
- printf ("%d\n", v >> 2);
+ printf("%d\n", v >> 2);
}
- else if ( consp (v) )
+ else if (consp(v))
{
- if ( listp (v) )
+ if (listp(v))
{
- printf ("list:\n");
+ printf("list:\n");
- for ( value_t n = v; !nilp (n); n = cdr (n) )
+ for (value_t n = v; !nilp(n); n = cdr(n))
{
- printval (car (n), depth + 1);
+ printval(car(n), depth + 1);
}
}
else
{
- printf ("cons:\n");
- printval (car (v), depth + 1);
- printval (cdr (v), depth + 1);
+ printf("cons:\n");
+ printval(car(v), depth + 1);
+ printval(cdr(v), depth + 1);
}
}
- else if ( nilp (v) )
+ else if (nilp(v))
{
- printf ("nil\n");
+ printf("nil\n");
}
else
{
- printf ("<unknown %d>\n", v);
+ printf("<unknown %d>\n", v);
}
}
-bool readlist (struct istream *is, value_t *val)
+bool readlist(struct istream *is, value_t *val)
{
- skipws (is);
+ skipws(is);
- if ( is->peek (is) != '(' )
+ if (is->peek(is) != '(')
return false;
- is->get (is);
+ is->get(is);
- *val = readn (is);
+ *val = readn(is);
- if ( is->peek (is) != ')' )
+ if (is->peek(is) != ')')
{
- is->showpos (is, stderr);
- err ("Unterminated list");
+ is->showpos(is, stderr);
+ err("Unterminated list");
return false;
}
- is->get (is);
+ is->get(is);
return true;
}
-bool readint (struct istream *is, value_t *val)
+bool readint(struct istream *is, value_t *val)
{
int number = 0;
- if ( !isdigit (is->peek (is)) )
+ if (!isdigit(is->peek(is)))
return false;
- while ( isdigit (is->peek (is)) )
+ while (isdigit(is->peek(is)))
{
number *= 10;
- number += is->get (is) - '0';
+ number += is->get(is) - '0';
}
- *val = intval (number);
+ *val = intval(number);
return true;
}
-bool read1 (struct istream *is, value_t *val)
+bool read1(struct istream *is, value_t *val)
{
- if ( readsym (is, val) )
+ if (readsym(is, val))
return true;
- if ( readstr (is, val) )
+ if (readstr(is, val))
return true;
- if ( readint (is, val) )
+ if (readint(is, val))
return true;
- if ( readlist (is, val) )
+ if (readlist(is, val))
return true;
return false;
}
-value_t readn (struct istream *is)
+value_t readn(struct istream *is)
{
value_t first = nil;
value_t *last = &first;
value_t read_val;
- while ( read1 (is, &read_val) )
+ while (read1(is, &read_val))
{
- *last = cons (read_val, nil);
- last = cdrref (*last);
+ *last = cons(read_val, nil);
+ last = cdrref(*last);
}
return first;
@@ -292,27 +292,27 @@
int linestart;
};
-int stristream_peek (struct istream *is)
+int stristream_peek(struct istream *is)
{
struct stristream_private *p = is->data;
- if ( p->i < p->length )
- return p->val[ p->i ];
+ if (p->i < p->length)
+ return p->val[p->i];
else
return -1;
}
-int stristream_get (struct istream *is)
+int stristream_get(struct istream *is)
{
struct stristream_private *p = is->data;
- if ( p->i < p->length )
+ if (p->i < p->length)
{
- char c = p->val[ p->i++ ];
+ char c = p->val[p->i++];
p->fromleft++;
- if ( c == '\n' )
+ if (c == '\n')
{
p->fromleft = 1;
p->line++;
@@ -325,46 +325,46 @@
return -1;
}
-int stristream_read (struct istream *s, char *buffer, int size)
+int stristream_read(struct istream *s, char *buffer, int size)
{
struct stristream_private *p = s->data;
- int len = MIN (size, p->length - p->i);
- memcpy (buffer, p->val, len);
+ int len = MIN(size, p->length - p->i);
+ memcpy(buffer, p->val, len);
return len;
}
-void stristream_showpos (struct istream *s, FILE *out)
+void stristream_showpos(struct istream *s, FILE *out)
{
struct stristream_private *p = s->data;
- fprintf (out, "line: %d, char %d\n", p->line, p->fromleft);
+ fprintf(out, "line: %d, char %d\n", p->line, p->fromleft);
int end = p->length;
- for ( int i = p->linestart; i < p->length; i++ )
+ for (int i = p->linestart; i < p->length; i++)
{
- if ( p->val[ i ] == '\n' )
+ if (p->val[i] == '\n')
{
end = i;
break;
}
}
- fprintf (out, " | %.*s\n", end - p->linestart, p->val + p->linestart);
- fprintf (out, " | ");
- for ( int i = 0; i < p->fromleft - 1; i++ )
- fprintf (out, " ");
+ fprintf(out, " | %.*s\n", end - p->linestart, p->val + p->linestart);
+ fprintf(out, " | ");
+ for (int i = 0; i < p->fromleft - 1; i++)
+ fprintf(out, " ");
- fprintf (out, "\033[31m^\033[0m\n");
+ fprintf(out, "\033[31m^\033[0m\n");
}
-struct istream *new_stristream (char *str, int length)
+struct istream *new_stristream(char *str, int length)
{
- struct istream *is = malloc (sizeof (struct istream));
- struct stristream_private *p = malloc (sizeof (struct stristream_private));
+ struct istream *is = malloc(sizeof(struct istream));
+ struct stristream_private *p = malloc(sizeof(struct stristream_private));
- p->val = strndup (str, length);
+ p->val = strndup(str, length);
p->i = 0;
p->length = length;
p->line = 1;
@@ -380,136 +380,136 @@
return is;
}
-void del_stristream (struct istream *stristream)
+void del_stristream(struct istream *stristream)
{
struct stristream_private *p = stristream->data;
- free (p->val);
- free (p);
- free (stristream);
+ free(p->val);
+ free(p);
+ free(stristream);
}
-struct istream *new_stristream_nt (char *str)
+struct istream *new_stristream_nt(char *str)
{
- return new_stristream (str, strlen (str));
+ return new_stristream(str, strlen(str));
}
-bool startswith (struct istream *s, char *pattern)
+bool startswith(struct istream *s, char *pattern)
{
- char *check = strdup (pattern);
- s->read (s, check, strlen (pattern));
+ char *check = strdup(pattern);
+ s->read(s, check, strlen(pattern));
- bool res = strcmp (check, pattern) == 0;
- free (check);
+ bool res = strcmp(check, pattern) == 0;
+ free(check);
return res;
}
-value_t strval (char *str)
+value_t strval(char *str)
{
value_t v;
- char *a = malloc_aligned (strlen (str) + 1);
+ char *a = malloc_aligned(strlen(str) + 1);
v = (value_t)a;
v |= STRING_TAG;
return v;
}
-bool integerp (value_t v)
+bool integerp(value_t v)
{
return (v & INT_MASK) == INT_TAG;
}
-bool symbolp (value_t v)
+bool symbolp(value_t v)
{
return (v & HEAP_MASK) == SYMBOL_TAG;
}
-bool stringp (value_t v)
+bool stringp(value_t v)
{
return (v & HEAP_MASK) == STRING_TAG;
}
-bool consp (value_t v)
+bool consp(value_t v)
{
return (v & HEAP_MASK) == CONS_TAG;
}
-bool listp (value_t v)
+bool listp(value_t v)
{
value_t next = v;
- while ( consp (next) )
+ while (consp(next))
{
- next = cdr (next);
+ next = cdr(next);
}
- return nilp (next);
+ return nilp(next);
}
-value_t car (value_t v)
+value_t car(value_t v)
{
- if ( !consp (v) )
+ if (!consp(v))
return nil;
- return *carref (v);
+ return *carref(v);
}
-value_t cdr (value_t v)
+value_t cdr(value_t v)
{
- if ( !consp (v) )
+ if (!consp(v))
return nil;
- return *cdrref (v);
+ return *cdrref(v);
}
-value_t *carref (value_t v)
+value_t *carref(value_t v)
{
- if ( !consp (v) )
+ if (!consp(v))
return NULL;
struct cons *c = (void *)(v ^ CONS_TAG);
return &c->car;
}
-value_t *cdrref (value_t v)
+value_t *cdrref(value_t v)
{
- if ( !consp (v) )
+ if (!consp(v))
return NULL;
struct cons *c = (void *)(v ^ CONS_TAG);
return &c->cdr;
}
-bool nilp (value_t v)
+bool nilp(value_t v)
{
return v == nil;
}
-int length (value_t v)
+int length(value_t v)
{
int i = 0;
- for ( ; !nilp (v); v = cdr (v) )
+ for (; !nilp(v); v = cdr(v))
i++;
return i;
}
-value_t elt (value_t v, int index)
+value_t elt(value_t v, int index)
{
- for ( int i = 0; i < index; i++ )
+ for (int i = 0; i < index; i++)
{
- v = cdr (v);
+ v = cdr(v);
}
- return car (v);
+ return car(v);
}
-bool symstreq (value_t sym, char *str)
+bool symstreq(value_t sym, char *str)
{
- if ( (sym & HEAP_MASK) != SYMBOL_TAG )
+ if ((sym & HEAP_MASK) != SYMBOL_TAG)
return false;
- return strcmp ((char *) (sym ^ SYMBOL_TAG), str) == 0;
+ return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0;
}
diff --git a/src/lisp/lisp.h b/src/lisp/lisp.h
index ba69a87..d3a0a29 100644
--- a/src/lisp/lisp.h
+++ b/src/lisp/lisp.h
@@ -57,51 +57,51 @@
void *data;
// These two return -1 on error
- int (*peek) (struct istream *s);
- int (*get) (struct istream *s);
+ int (*peek)(struct istream *s);
+ int (*get)(struct istream *s);
- int (*read) (struct istream *s, char *buffer, int size);
+ int (*read)(struct istream *s, char *buffer, int size);
- void (*showpos) (struct istream *s, FILE *out);
+ void (*showpos)(struct istream *s, FILE *out);
};
-bool startswith (struct istream *s, char *pattern);
+bool startswith(struct istream *s, char *pattern);
-bool readsym (struct istream *is, value_t *val);
-bool readstr (struct istream *is, value_t *val);
-bool readlist (struct istream *is, value_t *val);
-bool readint (struct istream *is, value_t *val);
+bool readsym(struct istream *is, value_t *val);
+bool readstr(struct istream *is, value_t *val);
+bool readlist(struct istream *is, value_t *val);
+bool readint(struct istream *is, value_t *val);
-value_t intval (int i);
-value_t strval (char *str);
-value_t cons (value_t car, value_t cdr);
-bool read1 (struct istream *is, value_t *val);
-value_t read (struct istream *is);
-value_t readn (struct istream *is);
+value_t intval(int i);
+value_t strval(char *str);
+value_t cons(value_t car, value_t cdr);
+bool read1(struct istream *is, value_t *val);
+value_t read(struct istream *is);
+value_t readn(struct istream *is);
-value_t car (value_t v);
-value_t cdr (value_t v);
-value_t *carref (value_t v);
-value_t *cdrref (value_t v);
+value_t car(value_t v);
+value_t cdr(value_t v);
+value_t *carref(value_t v);
+value_t *cdrref(value_t v);
-bool integerp (value_t v);
-bool symbolp (value_t v);
-bool stringp (value_t v);
-bool consp (value_t v);
-bool listp (value_t v);
-bool nilp (value_t v);
-int length (value_t v);
-value_t elt (value_t v, int index);
+bool integerp(value_t v);
+bool symbolp(value_t v);
+bool stringp(value_t v);
+bool consp(value_t v);
+bool listp(value_t v);
+bool nilp(value_t v);
+int length(value_t v);
+value_t elt(value_t v, int index);
-void printval (value_t v, int depth);
+void printval(value_t v, int depth);
-struct istream *new_stristream (char *str, int length);
+struct istream *new_stristream(char *str, int length);
// same as above but null terminated
-struct istream *new_stristream_nt (char *str);
-void del_stristream (struct istream *stristream);
+struct istream *new_stristream_nt(char *str);
+void del_stristream(struct istream *stristream);
-void err (const char *msg);
+void err(const char *msg);
-bool symstreq (value_t sym, char *str);
+bool symstreq(value_t sym, char *str);
extern value_t nil;
diff --git a/src/lisp/main.c b/src/lisp/main.c
index 5c217fc..5679572 100644
--- a/src/lisp/main.c
+++ b/src/lisp/main.c
@@ -1,19 +1,19 @@
-#include "lisp.h"
#include "compiler.h"
+#include "lisp.h"
-int main (int argc, char **argv)
+int main(int argc, char **argv)
{
- if ( argc < 2 )
+ if (argc < 2)
{
- puts ("pass the string you want to parse as the first argument please");
+ puts("pass the string you want to parse as the first argument please");
return 1;
}
- struct istream *is = new_stristream_nt (argv[ 1 ]);
+ struct istream *is = new_stristream_nt(argv[1]);
- struct environment env = compile_all (is);
- value_t (*lisp_main) () = find_function(&env, "main")->def0;
- lisp_main ();
+ struct environment env = compile_all(is);
+ value_t (*lisp_main)() = find_function(&env, "main")->def0;
+ lisp_main();
-// del_stristream (is);
+ // del_stristream (is);
}
diff --git a/src/lisp/plat/linux.c b/src/lisp/plat/linux.c
index bd9af10..83cc39b 100644
--- a/src/lisp/plat/linux.c
+++ b/src/lisp/plat/linux.c
@@ -3,41 +3,42 @@
#include <string.h>
#include <sys/mman.h>
-void *malloc_aligned (size_t size)
+void *malloc_aligned(size_t size)
{
- void *mem = malloc (size + 8 + sizeof (void *) * 2);
- void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7);
- aligned_ptr[ -1 ] = mem;
- aligned_ptr[ -2 ] = (void *) size;
+ void *mem = malloc(size + 8 + sizeof(void *) * 2);
+ void **aligned_ptr = (void **)((uintptr_t)(mem + 8 + sizeof(void *)) & ~7);
+ aligned_ptr[-1] = mem;
+ aligned_ptr[-2] = (void *)size;
return aligned_ptr;
}
-void *realloc_aligned (void *addr, size_t size)
+void *realloc_aligned(void *addr, size_t size)
{
- void *mem = malloc (size + 8 + sizeof (void *) * 2);
- void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7);
- aligned_ptr[ -1 ] = mem;
+ void *mem = malloc(size + 8 + sizeof(void *) * 2);
+ void **aligned_ptr = (void **)((uintptr_t)(mem + 8 + sizeof(void *)) & ~7);
+ aligned_ptr[-1] = mem;
- memcpy (aligned_ptr, addr, (uintptr_t) aligned_ptr[ -2 ]);
-
+ memcpy(aligned_ptr, addr, (uintptr_t)aligned_ptr[-2]);
+
return aligned_ptr;
}
-void free_aligned (void *addr)
+void free_aligned(void *addr)
{
void **ptr = (void **)addr;
- free (ptr[ -1 ]);
+ free(ptr[-1]);
}
-void *link (dasm_State **Dst)
+void *link(dasm_State **Dst)
{
size_t size;
void *buf;
- dasm_link (Dst, &size);
- buf = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- dasm_encode (Dst, buf);
+ dasm_link(Dst, &size);
+ buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
+ 0);
+ dasm_encode(Dst, buf);
- mprotect (buf, size, PROT_READ | PROT_EXEC);
+ mprotect(buf, size, PROT_READ | PROT_EXEC);
return buf;
}
diff --git a/src/lisp/plat/plat.h b/src/lisp/plat/plat.h
index 87544ba..f10676c 100644
--- a/src/lisp/plat/plat.h
+++ b/src/lisp/plat/plat.h
@@ -1,14 +1,14 @@
#pragma once
-#include <stdint.h>
-#include <stddef.h>
#include <dasm_proto.h>
+#include <stddef.h>
+#include <stdint.h>
/* Platform specific definitions */
// Must return an address aligned to 8 bytes
-void *malloc_aligned (size_t size);
-void *realloc_aligned (void *addr, size_t size);
-void free_aligned (void *addr);
+void *malloc_aligned(size_t size);
+void *realloc_aligned(void *addr, size_t size);
+void free_aligned(void *addr);
-void *link (dasm_State **Dst);
+void *link(dasm_State **Dst);
diff --git a/src/lisp/test.lisp b/src/lisp/test.lisp
index 68cc615..82e5955 100644
--- a/src/lisp/test.lisp
+++ b/src/lisp/test.lisp
@@ -3,4 +3,5 @@
(defun main ()
(print "64 / (2 + 2) =")
- (print (/ 64 (two-plus-two))))
+ (print (/ 64 (two-plus-two)))
+ (print nil))