Skip to content

Commit

Permalink
Most of a C implementation of jq
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen committed Aug 16, 2012
1 parent eca89ac commit 2002dc1
Show file tree
Hide file tree
Showing 16 changed files with 1,270 additions and 0 deletions.
21 changes: 21 additions & 0 deletions c/Makefile
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
18 changes: 18 additions & 0 deletions c/builtin.c
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])};
8 changes: 8 additions & 0 deletions c/builtin.h
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
35 changes: 35 additions & 0 deletions c/bytecode.c
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);
}
}
}
33 changes: 33 additions & 0 deletions c/bytecode.h
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
Loading

0 comments on commit 2002dc1

Please sign in to comment.