Skip to content

Commit

Permalink
Type conversions.
Browse files Browse the repository at this point in the history
- Conversion between numeric types
- Scalar functions working

TODO
  + Change noun parsing to use numeric conversions
  • Loading branch information
sazl committed Aug 12, 2010
1 parent 0c11bc8 commit f9b6d7b
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 129 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

OBJS= memory.o util.o error.o atom.o noun.o function.o verb.o verb-scalar1.o \
verb-scalar2.o verb-atomic.o primitive.o adverb.o conjunction.o \
lexer.o parser.o test.o anicca.o
OBJS= memory.o util.o error.o convert.o atom.o noun.o function.o verb.o \
verb-scalar1.o verb-scalar2.o verb-atomic.o primitive.o adverb.o \
conjunction.o lexer.o parser.o test.o anicca.o

CFLAGS=-ansi -pedantic -g3

Expand All @@ -24,6 +24,7 @@ primitive.o: primitive.c primitive.h verb.h
function.o: function.c function.h
noun.o: noun.c noun.h util.h
atom.o: atom.c atom.h noun.h
convert.o: convert.c convert.h
error.o: error.c error.h
util.o: util.c util.h
memory.o: memory.c memory.h
Expand Down
8 changes: 6 additions & 2 deletions anicca.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "anicca.h"
#include "char.h"
#include "memory.h"
#include "convert.h"
#include "function.h"
#include "verb.h"
#include "adverb.h"
Expand All @@ -13,15 +15,17 @@
#include "parser.h"
#include "util.h"


I main(I argc, C *argv[]) {
C str[100];
C *v, str[100];
A x, y, z;

a_init();
while (1) {
printf(" ");
fgets(str, 100, stdin);
println(z = eval(str));
v = strndup(str, strlen(str)-1); /* remove carriage return */
println(z = eval(v));
}
return 0;
}
30 changes: 15 additions & 15 deletions anicca.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ typedef A(*AF1)(A);
typedef A(*AF2)(A, A);
typedef A(*AF3)(A, A, A);

#define ANY -1
#define BOOL (1<<1)
#define CHAR (1<<2)
#define INT (1<<3)
#define FLT (1<<4)
#define CMPX (1<<5)
#define BOX (1<<6)
#define VERB (1<<7)
#define ADV (1<<8)
#define CONJ (1<<9)
#define NAME (1<<10)
#define LPAR (1<<11)
#define RPAR (1<<12)
#define ASGN (1<<13)
#define MARK (1<<14)
#define ANY -1L
#define BOOL (1L<<0)
#define CHAR (1L<<1)
#define INT (1L<<2)
#define FLT (1L<<3)
#define CMPX (1L<<4)
#define BOX (1L<<5)
#define VERB (1L<<6)
#define ADV (1L<<7)
#define CONJ (1L<<8)
#define NAME (1L<<9)
#define LPAR (1L<<10)
#define RPAR (1L<<11)
#define ASGN (1L<<12)
#define MARK (1L<<13)

#define NUMERIC (BOOL | INT | FLT | CMPX)
#define NOUN (NUMERIC | CHAR | BOX)
Expand Down
30 changes: 15 additions & 15 deletions conjunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ DYAD(amper) {

if ((xt&VERB && yt&NOUN)||(xt&NOUN && yt&VERB)) {
v = VAV(xt&VERB ? x : y);
z = CDERV(CAMPR, bond, dbond, x, y, VLR(v), VMR(v), VRR(v));
z = CDERV(CAMPR, bond, bond2, x, y, VLR(v), VMR(v), VRR(v));
}
else if (xt&VERB && yt&&VERB) {
v = VAV(y);
z = CDERV(CAMPR, compose, dcompose, x, y, VLR(v), VMR(v), VRR(v));
z = CDERV(CAMPR, compose, compose2, x, y, VLR(v), VMR(v), VRR(v));
}
return z;
}
Expand All @@ -26,7 +26,7 @@ DMONAD(bond) { DECL_FG;
return z;
}

DDYAD(dbond) { DECL_FG;
DDYAD(bond2) { DECL_FG;
return z;
}

Expand All @@ -35,16 +35,16 @@ DMONAD(compose) { DECL_FG;
return z;
}

DDYAD(dcompose) { DECL_FG;
DDYAD(compose2) { DECL_FG;
z = df2(df1(x, g), df1(y, g), f);
return z;
}

DYAD(at) {
DYAD(atsign) {
V *v; A z;
if (AT(x)&VERB && AT(y)&VERB) {
v = VAV(y);
z = CDERV(CAT, atop, datop, x, y, VLR(v), VMR(v), VRR(v));
z = CDERV(CAT, atop, atop2, x, y, VLR(v), VMR(v), VRR(v));
}
return z;
}
Expand All @@ -55,51 +55,51 @@ DMONAD(atop) {
return z;
}

DDYAD(datop) { DECL_FG;
DDYAD(atop2) { DECL_FG;
z = df1(df2(x, y, g), f);
return z;
}

DMONAD(chook) { DECL_FG;
DMONAD(hook) { DECL_FG;
z = df2(y, df1(y, g), f);
return z;
}

DDYAD(dchook) { DECL_FG;
DDYAD(hook2) { DECL_FG;
z = df2(x, df1(y, g), f);
return z;
}

DMONAD(cfork) { DECL_FGH;
DMONAD(forrk) { DECL_FGH;
z = (AT(f)&VERB && VID(VAV(f))==CCAP) ? df1(df1(y, h), g) :
AT(f)&NOUN ? df2(f, df1(y, h), g) :
df2(df1(y, f), df1(y, h), g);
return z;
}

DDYAD(dcfork) { DECL_FGH;
DDYAD(forrk2) { DECL_FGH;
z = (AT(f)&VERB && VID(VAV(f))==CCAP) ? df1(df2(x, y, h), g) :
AT(f)&NOUN ? df2(f, df2(x, y, h), g) :
df2(df2(x, y, f), df2(x, y, h), g);
return z;
}

DYAD(powr) {
DYAD(cartcol) {
V *v; A z; I xt = AT(x), yt = AT(y);
ASSERT(xt&VERB && yt&INT, ERDOM);
v = VAV(xt&VERB ? x : y);
z = CDERV(CPOWR, cpower, cdpower, x, y, VLR(v), VMR(v), VRR(v));
z = CDERV(CPOWR, power, power2, x, y, VLR(v), VMR(v), VRR(v));
return z;
}

DMONAD(cpower) { DECL_FG;
DMONAD(power) { DECL_FG;
I n = *IAV(g); A temp;
DO(n, temp = y; y = df1(temp, f); a_free(temp));
z = y;
return z;
}

DDYAD(cdpower) { DECL_FG;
DDYAD(power2) { DECL_FG;
I n = *IAV(g); A temp;
DO(n, temp = y; y = df2(x, temp, f); a_free(temp));
z = y;
Expand Down
22 changes: 11 additions & 11 deletions conjunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

DYAD(amper);
DMONAD(bond);
DDYAD(dbond);
DDYAD(bond2);
DMONAD(compose);
DDYAD(dcompose);
DYAD(at);
DDYAD(compose2);
DYAD(atsign);
DMONAD(atop);
DDYAD(datop);
DMONAD(chook);
DDYAD(dchook);
DYAD(powr);
DMONAD(cpower);
DDYAD(cdpower);
DMONAD(cfork);
DDYAD(dcfork);
DDYAD(atop2);
DMONAD(hook);
DDYAD(hook2);
DYAD(cartcol);
DMONAD(power);
DDYAD(power2);
DMONAD(forrk);
DDYAD(forrk2);

#endif
18 changes: 18 additions & 0 deletions convert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdlib.h>

#include "anicca.h"
#include "memory.h"
#include "convert.h"

A convert(I t, A y) {
I yt = AT(y), yr = AR(y), yn = AN(y), *ys = AS(y);
I k = type_size(t), *iv; B *bv; D *dv;
A z = gen_array(t, yr, yn, ys);

switch(k = CNVCASE(yt, t)) {
case BtI: bv=BAV(y); iv=IAV(z); DO(yn, iv[i] = (I)bv[i]); break;
case BtD: bv=BAV(y); dv=DAV(z); DO(yn, dv[i] = (D)bv[i]); break;
case ItD: iv=IAV(y); dv=DAV(z); DO(yn, dv[i] = (D)iv[i]); break;
}
return z;
}
21 changes: 21 additions & 0 deletions convert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _CONVERT_H

/*
Conversion vector:
lowest 8 bits: argument type.
highest 8 bits: result type.
ItD: 0 0 0 0 1 0 0 0 | 0 0 0 0 0 1 0 0
FLT INT
*/

#define CNVCASE(a,b) ((a)+(VERB*(b)))
#define BtI CNVCASE(BOOL, INT)
#define BtD CNVCASE(BOOL, FLT)
#define BtZ CNVCASE(BOOL, CMPX)
#define ItD CNVCASE(INT, FLT)
#define ItZ CNVCASE(INT, CMPX)
#define DtZ CNVCASE(FLT, CMPX)

A convert(I t, A y);

#endif
5 changes: 3 additions & 2 deletions function.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>

#include "anicca.h"
#include "char.h"
#include "memory.h"
Expand All @@ -19,13 +20,13 @@ DDYAD(df2) {

DMONAD(dhk) {
V *v = VAV(y);
return derv_def(CHOOK, VERB, chook, dchook, y, self, NULL, VLR(v), VMR(v), \
return derv_def(CHOOK, VERB, hook, hook2, y, self, NULL, VLR(v), VMR(v), \
VRR(v), 0);
}

DDYAD(dfrk) {
V *v = VAV(x);
return derv_def(CFORK, VERB, cfork, dcfork, x, y, self, VLR(v), VMR(v), \
return derv_def(CFORK, VERB, forrk, forrk2, x, y, self, VLR(v), VMR(v), \
VRR(v), 0);
}

Expand Down
15 changes: 4 additions & 11 deletions noun.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ PARSE(num) {
NT(a) = FLT; }
else {
iv = strtol(s, &e, 10);
if (iv==0 || ABS(iv)==1) {
NB(a) = si ? -(B)iv : (B)iv;
NT(a) = BOOL; }
if (!si && (iv==0 || ABS(iv)==1)) { NB(a) = (B)iv; NT(a) = BOOL; }
else { NI(a) = si ? -iv : iv; NT(a) = INT; }
}
return e;
Expand All @@ -163,18 +161,13 @@ PARSE(num) {
output: Numeric array.
*/
A parse_noun(I n, C *s) {
A y = noun_index(n+1, s), z;
B *bv;
I al, as, m = AN(y)/2, j, k = 0, t = 0, *indx = IAV(y), *iv;
D *dv;
Z *zv;
B *bv; D *dv; Z *zv;
A y = noun_index(n+1, s), z;
N *atm, *nouns = (N *)a_malloc(sizeof(N)*m);

DO(m,
j = i+i;
as = indx[j];
al = indx[j+1];
atm = &nouns[i];
j = i+i; as = indx[j]; al = indx[j+1]; atm = &nouns[i];
ASSERT(parse_atom(al, &s[as], atm), ERLEXER);
t = MAX(t, NT(atm));
);
Expand Down
4 changes: 2 additions & 2 deletions primitive.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static P primitives[NPRIM+1] = {
/* 18 = 61 */ {VERB, NULL, equal, 0, 0, 0, 0},
/* 19 > 62 */ {VERB, NULL, gthan, 0, 0, 0, 0},
/* 20 ? 63 */ {VERB, NULL, NULL, 0, 0, 0, 0},
/* 21 @ 64 */ {CONJ, NULL, at, 0, 0, 0, 0},
/* 21 @ 64 */ {CONJ, NULL, atsign, 0, 0, 0, 0},
/* 22 A. 65 */ {VERB, NULL, NULL, 0, 0, 0, 0},
/* 23 C. 66 */ {VERB, NULL, NULL, 0, 0, 0, 0},
/* 24 D. 68 */ {CONJ, NULL, NULL, 0, 0, 0, 0},
Expand Down Expand Up @@ -189,7 +189,7 @@ static P primitives[NPRIM+1] = {
/* 107 t: 157 */ {ADV, NULL, NULL, 0, 0, 0, 0},
/* 108 u: 158 */ {VERB, NULL, NULL, 0, 0, 0, 0},
/* 109 x: 159 */ {VERB, NULL, NULL, 0, 0, 0, 0},
/* 110 ^: 160 */ {CONJ, NULL, powr, 0, 0, 0, 0},
/* 110 ^: 160 */ {CONJ, NULL, cartcol, 0, 0, 0, 0},
/* 111 `: 161 */ {CONJ, NULL, NULL, 0, 0, 0, 0},
/* 112 {: 162 */ {VERB, tail, NULL, 0, 0, 0, 0},
/* 113 |: 163 */ {VERB, NULL, NULL, 0, 0, 0, 0},
Expand Down
5 changes: 2 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ VO println(A y) {
}

VO a_init(VO) {
zero = scalar_int(0);
one = scalar_int(1);
zero = scalar_int(0); one = scalar_int(1);
mark = gen_array(MARK, 0, 0, NULL);
lpar = gen_array(LPAR, 0, 0, NULL);
rpar = gen_array(RPAR, 0, 0, NULL);
}

A eval(const C *str) {
A w, x, y, z;
w = gen_str(strlen(str), str);
w = gen_str(strlen(str)+1, str);
x = token_index(w);
y = tokens(x, w);
z = parse(y);
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
VO print(A y);
VO println(A y);
VO a_init(VO);
C *strndup(const C *s, UI n);
A eval(const C *str);

#endif
Loading

0 comments on commit f9b6d7b

Please sign in to comment.