-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stephen
committed
Aug 16, 2012
1 parent
eca89ac
commit 2002dc1
Showing
16 changed files
with
1,270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CC=gcc -Wall -std=gnu99 -ggdb | ||
|
||
.PHONY: all clean | ||
all: parsertest | ||
|
||
clean: | ||
make -Bnd | grep 'Must remake target' | \ | ||
sed 's/.*`\(.*\)'\''.*/\1/' | grep -v '^all$$' | \ | ||
xargs rm | ||
|
||
|
||
lexer.yy.c: lexer.l | ||
flex -o lexer.yy.c --header-file=lexer.yy.h lexer.l | ||
lexer.yy.h: lexer.yy.c | ||
|
||
parser.tab.c: parser.y lexer.yy.h | ||
bison -W -d parser.y | ||
parser.tab.h: parser.tab.c | ||
|
||
parsertest: parser.tab.c lexer.yy.c main.c opcode.c bytecode.c compile.c execute.c builtin.c | ||
$(CC) -o $@ $^ -ljansson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "builtin.h" | ||
|
||
#include <jansson.h> | ||
|
||
|
||
void f_false(json_t* input[], json_t* output[]) { | ||
output[0] = json_false(); | ||
} | ||
|
||
void f_true(json_t* input[], json_t* output[]) { | ||
output[0] = json_true(); | ||
} | ||
|
||
struct cfunction function_list[] = { | ||
{f_true, "true", CALL_BUILTIN_1_1}, | ||
{f_false, "false", CALL_BUILTIN_1_1}, | ||
}; | ||
struct symbol_table builtins = {function_list, sizeof(function_list)/sizeof(function_list[0])}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef BUILTIN_H | ||
#define BUILTIN_H | ||
|
||
#include "bytecode.h" | ||
|
||
extern struct symbol_table builtins; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <jansson.h> | ||
#include "bytecode.h" | ||
#include "opcode.h" | ||
|
||
void dump_disassembly(struct bytecode* bc) { | ||
int pc = 0; | ||
while (pc < bc->codelen) { | ||
dump_operation(bc, bc->code + pc); | ||
printf("\n"); | ||
pc += opcode_length(bc->code[pc]); | ||
} | ||
} | ||
|
||
void dump_operation(struct bytecode* bc, uint16_t* codeptr) { | ||
int pc = codeptr - bc->code; | ||
printf("%04d ", pc); | ||
const struct opcode_description* op = opcode_describe(bc->code[pc++]); | ||
printf("%s", op->name); | ||
if (op->flags & OP_HAS_IMMEDIATE) { | ||
uint16_t imm = bc->code[pc++]; | ||
printf(" "); | ||
if (op->flags & OP_HAS_BRANCH) { | ||
printf("%04d", pc + imm); | ||
} else if (op->flags & OP_HAS_CONSTANT) { | ||
json_dumpf(json_array_get(bc->constants, imm), | ||
stdout, JSON_ENCODE_ANY); | ||
} else if (op->flags & OP_HAS_VARIABLE) { | ||
printf("v%d", imm); | ||
} else { | ||
printf("%d", imm); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef BYTECODE_H | ||
#define BYTECODE_H | ||
#include <jansson.h> | ||
#include <stdint.h> | ||
|
||
#include "opcode.h" | ||
|
||
typedef void (*cfunction_ptr)(json_t* input[], json_t* output[]); | ||
|
||
struct cfunction { | ||
cfunction_ptr fptr; | ||
const char* name; | ||
opcode callop; | ||
}; | ||
|
||
#define MAX_CFUNCTION_ARGS 10 | ||
struct symbol_table { | ||
struct cfunction* cfunctions; | ||
int ncfunctions; | ||
}; | ||
|
||
|
||
struct bytecode { | ||
uint16_t* code; | ||
int codelen; | ||
int framesize; | ||
json_t* constants; | ||
struct symbol_table* globals; | ||
}; | ||
|
||
void dump_disassembly(struct bytecode* code); | ||
void dump_operation(struct bytecode* bc, uint16_t* op); | ||
#endif |
Oops, something went wrong.