vsm2

exeprimental virtual stack machine for *nix
Log | Files | Refs

repl.c (2685B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <unistd.h>
      4 #include <string.h>
      5 
      6 #include "cell.h"
      7 #include "bus.h"
      8 #include "mem.h"
      9 #include "util.h"
     10 
     11 #define StackSize 256
     12 struct {
     13 	Cell mem[StackSize];
     14 	unsigned int n;
     15 } stack;
     16 
     17 void push(Cell *);
     18 Cell * pop(void);
     19 
     20 Bus *bus;
     21 
     22 #define CMD(x) void x(void)
     23 CMD(str);
     24 CMD(hex);
     25 CMD(top);
     26 CMD(drp);
     27 CMD(hlp);
     28 CMD(snd);
     29 CMD(rcv);
     30 
     31 struct {
     32 	const char *cmd;
     33 	void (*fp)(void);
     34 } dict[]= {
     35 	{"str", str},
     36 	{"hex", hex},
     37 	{"top", top},
     38 	{"drp", drp},
     39 	{"hlp", hlp},
     40 	{"snd", snd},
     41 	{"rcv", rcv},
     42 };
     43 
     44 void
     45 repl(void)
     46 {
     47 	long n;
     48 	char buf[1024];
     49 	int i;
     50 	printf("> ");
     51 	fflush(stdout);
     52 	while((n = read(0, buf, 1024)) > 0){
     53 		void (*fp)(void) = NULL;
     54 		for (i = 0; i < sizeof(dict)/sizeof(dict[0]); i++){
     55 			if (strncmp(dict[i].cmd, buf, 3) == 0)
     56 				fp = dict[i].fp;
     57 		}
     58 		if (fp == NULL) printf("?\n");
     59 		else fp();
     60 		printf("> ");
     61 		fflush(stdout);
     62 	}
     63 }
     64 
     65 int
     66 main(void)
     67 {
     68 	bus = createbus();
     69 	repl();
     70 	return 0;
     71 }
     72 
     73 void
     74 str(void)
     75 {
     76 	long n;
     77 	char buf[1024];
     78 	printf("` ");
     79 	fflush(stdout);
     80 	n = read(0, buf, 1024);
     81 	buf[n-1] = '\0';
     82 	Cell *c = createcell(n, NULL);
     83 	c->p = malloc(n);
     84 	memcpy(c->p, buf, n);
     85 	push(c);
     86 }
     87 
     88 void
     89 hex(void)
     90 {
     91 	long n;
     92 	char buf[1024];
     93 	long long x;
     94 	printf("0x");
     95 	fflush(stdout);
     96 	n = read(0, buf, 1024);
     97 	buf[n-1] = '\0';
     98 	x = strtoll(buf, 0, 16);
     99 
    100 	Cell *c = createcell(sizeof(long long), NULL);
    101 	c->p = malloc(c->n);
    102 	memcpy(c->p, &x, c->n);
    103 	push(c);
    104 }
    105 
    106 void
    107 hlp(void)
    108 {
    109 	int i;
    110 	for (i = 0; i < sizeof(dict) / sizeof(dict[0]); i++) {
    111 		printf("%s ", dict[i].cmd);
    112 	}
    113 	printf("\n");
    114 }
    115 
    116 void
    117 top(void)
    118 {
    119 	unsigned int n;
    120 	n = stack.n - 1;
    121 	if (n >= StackSize) n = StackSize - 1;
    122 	Cell *c = &stack.mem[n];
    123 	printf("[%02d] ", n);
    124 	hexdump(c->n, c->p);
    125 	printf("\n");
    126 }
    127 
    128 void
    129 drp(void)
    130 {
    131 	Cell *c = pop();
    132 	freecell(c);
    133 }
    134 
    135 void
    136 snd(void)
    137 {
    138 	/* send a message to the bus */
    139 	Cell *dat, *dstcell;
    140 	int dst;
    141 
    142 	dstcell = pop();
    143 	dst = ((int*)dstcell->p)[0];
    144 	freecell(dstcell);
    145 	dat = pop();
    146 
    147 	Msg *msg;
    148 	msg = createmsg(ChDebug, dst, dat);
    149 	buspush(bus, msg);
    150 }
    151 
    152 void
    153 rcv(void)
    154 {
    155 	/* receive a message from the bus */
    156 /*	int dst;
    157 	Cell *dstcell;
    158 	dstcell = pop();
    159 	dst = ((int*)dstcell->p)[0];
    160 	freecell(dstcell);
    161 */
    162 	Msg *msg;
    163 	msg = buspull(bus, ChDebug);
    164 	if (msg == NULL) printf("rcv: NULL\n");
    165 	else printf("rcv: %d %s\n", msg->src, msg->dat->p);
    166 	freemsg(msg);
    167 }
    168 
    169 void
    170 push(Cell *new)
    171 {
    172 	stack.mem[stack.n] = *new;
    173 	free(new);
    174 	stack.n++;
    175 	if (stack.n >= StackSize) stack.n = 0;
    176 }
    177 
    178 Cell *
    179 pop(void)
    180 {
    181 	stack.n--;
    182 	if (stack.n >= StackSize) stack.n = StackSize - 1;
    183 	Cell *c = celldup(&stack.mem[stack.n]);
    184 	free(stack.mem[stack.n].p);
    185 	stack.mem[stack.n].p = NULL;
    186 	stack.mem[stack.n].n = 0;
    187 	return c;
    188 }
    189