--- /dev/null
+OBJS=vm.o main.o error.o
+CFLAGS=-c -g
+
+vm_and_as: vm as
+
+as: as.o lexer error.o
+ $(CC) as.o aslexer.o -o as -lfl
+
+as.o: as.c
+ $(CC) $(CFLAGS) as.c
+
+lexer: aslexer.l
+ lex -i -o aslexer.c aslexer.l
+ $(CC) -c aslexer.c
+
+vm: $(OBJS)
+ $(CC) $(OBJS) -o vm
+
+vm.o: vm.c vm.h
+ $(CC) $(CFLAGS) vm.c
+
+main.o: main.c
+ $(CC) $(CFLAGS) main.c
+
+
+error.o: error.c error.h
+ $(CC) $(CFLAGS) error.c
+
+clean:
+ rm *.o
+ rm as vm
--- /dev/null
+miniforth
+
+at the moment, there is only a minimal vm implemented.
+see as.c and vm.c to see the assembler and the vm
+source code.
--- /dev/null
+/* a very crude assembler for the vm - it's one pass and
+ * spits out error messages the ken thompson way
+ * */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include "vm.h"
+#include "tokens.h"
+#include "error.h"
+
+extern FILE* yyin;
+extern char* yytext;
+extern int yylex();
+FILE *outfp;
+
+int setup_parser(char *filename) {
+ yyin = fopen(filename, "r");
+ if (yyin == NULL)
+ error("Unable to open input file");
+
+ outfp = fopen("a.bin", "w+");
+ if (outfp == NULL)
+ error("Unable to open output file");
+
+ return 0;
+}
+
+int parse() {
+ int t;
+ while((t = yylex()) != -1) {
+ switch(t) {
+ case T_PUSH:
+ yylex();
+ fputc(PUSH, outfp);
+ fputc(atoi(yytext), outfp);
+ break;
+
+ case T_POP:
+ yylex();
+ fputc(POP, outfp);
+ fputc(atoi(yytext), outfp);
+ break;
+
+ case T_DPRINT:
+ fputc(DPRINT, outfp);
+ break;
+
+ case T_NOP:
+ fputc(NOP, outfp);
+ break;
+
+ default:
+ printf("unexpected %d in input", t);
+ break;
+ }
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc != 2) {
+ puts("as file.as");
+ exit(0);
+ }
+
+ setup_parser(argv[1]);
+ parse();
+}
+
--- /dev/null
+/* a lexer for the assembler */
+%{
+ #include "tokens.h"
+%}
+
+%%
+
+<<EOF>> { return -1; }
+"push" { return T_PUSH; }
+"pop" { return T_POP; }
+"nop" { return T_NOP; }
+"dprint" { return T_DPRINT; }
+[0-9]+ { return NUMBER; }
+; { return COMMA; }
+[a-zA-Z][a-zA-Z0-9]* { return WORD; }
+
--- /dev/null
+#include "error.h"
+
+void error(char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+ puts("");
+ fflush(stdout);
+ exit(EXIT_FAILURE);
+}
+
--- /dev/null
+#ifndef __ERROR_H__
+#define __ERROR_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#endif
+
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "vm.h"
+
+int filelength(FILE *fp) {
+ if (fp == NULL)
+ return -1;
+
+ struct stat s;
+ fstat(fileno(fp), &s);
+ return (int) s.st_size;
+}
+
+int main(int argc, char **argv) {
+ if (argc != 2) {
+ puts("vm file.bin");
+ exit(0);
+ }
+
+ FILE *fp = fopen(argv[1], "r");
+ if (fp == NULL)
+ error("unable to find the requested file");
+
+ int flen = filelength(fp);
+
+ struct vm *v = alloc_vm(flen, 234);
+ load_file(v, fp);
+
+ printf("vm launched, memsize: %d, stacksize: %d\n", v->memsize, v->stacksize);
+ run(v);
+
+}
--- /dev/null
+NOP
+NOP
+PUSH 23
+DPRINT
--- /dev/null
+#ifndef __TOKENS_H__
+#define __TOKENS_H__
+
+enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT};
+
+#endif
--- /dev/null
+#include "vm.h"
+
+struct vm* alloc_vm(int memsize, int stacksize)
+{
+ struct vm *v = calloc(1, sizeof(struct vm));
+ if (v == NULL)
+ error("Unable to allocate virtual machine");
+
+ v->mem = calloc(memsize, sizeof(int));
+ if (v->mem == NULL)
+ error("Unable to allocate memory for vm");
+
+ v->stack = calloc(stacksize, sizeof(int));
+ if (v->stack == NULL)
+ error("Unable to allocate memory for vm stack");
+
+ v->memsize = memsize;
+ v->stacksize = stacksize;
+ return v;
+}
+
+int load_file(struct vm *v, FILE *fd)
+{
+ if (fd == NULL || v == NULL)
+ error("Unable to load file");
+
+ fread(v->mem, sizeof(char), v->memsize, fd);
+}
+
+int run(struct vm *v)
+{
+ if (v == NULL)
+ return -1;
+
+ int *ip = v->mem;
+ while(ip < v->mem + v->memsize) {
+ switch(*ip) {
+ case NOP:
+ break;
+
+ case PUSH:
+ puts("ho");
+ if (v->stackoffset >= v->stacksize)
+ error("stack overflow");
+
+ ip++;
+ printf("%d", *ip);
+ v->stack[v->stackoffset] = *ip;
+ v->stackoffset++;
+ break;
+
+ case POP:
+ if (v->stackoffset == 0)
+ error("stack underflow");
+
+ v->stackoffset--;
+ break;
+
+ case DPRINT:
+ /* a debug instruction - print the top of the stack */
+ printf("%d\n", v->stack[v->stackoffset]);
+ break;
+ }
+
+ ip++;
+ }
+}
--- /dev/null
+#ifndef __VM_H__
+#define __VM_H__
+
+#include <stdlib.h>
+#include <stdio.h>
+
+enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT};
+
+struct vm {
+ int *mem;
+ int memsize;
+ int *stack;
+ int stacksize;
+ int stackoffset;
+};
+
+struct vm* alloc_vm(int memsize, int stacksize);
+
+#endif