new curly bracket coding convention
This commit is contained in:
+39
-38
@@ -42,10 +42,10 @@ fun parse(str) {
|
|||||||
return none;
|
return none;
|
||||||
}
|
}
|
||||||
|
|
||||||
return parseAnyValue(ctx);
|
return parseAny(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseArrayValue(ctx) {
|
fun parseArray(ctx) {
|
||||||
|
|
||||||
assert(current(ctx) == '[');
|
assert(current(ctx) == '[');
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ fun parseArrayValue(ctx) {
|
|||||||
|
|
||||||
while true: {
|
while true: {
|
||||||
|
|
||||||
child = parseAnyValue(ctx);
|
child = parseAny(ctx);
|
||||||
|
|
||||||
if child == none:
|
if child == none:
|
||||||
return none;
|
return none;
|
||||||
@@ -103,7 +103,7 @@ fun parseArrayValue(ctx) {
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseObjectValue(ctx) {
|
fun parseObject(ctx) {
|
||||||
|
|
||||||
assert(current(ctx) == '{');
|
assert(current(ctx) == '{');
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ fun parseObjectValue(ctx) {
|
|||||||
return none;
|
return none;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = parseStringValue(ctx);
|
key = parseString(ctx);
|
||||||
|
|
||||||
if key == none:
|
if key == none:
|
||||||
return none;
|
return none;
|
||||||
@@ -146,7 +146,7 @@ fun parseObjectValue(ctx) {
|
|||||||
|
|
||||||
skip(ctx, isSpace);
|
skip(ctx, isSpace);
|
||||||
|
|
||||||
child = parseAnyValue(ctx);
|
child = parseAny(ctx);
|
||||||
|
|
||||||
if child == none:
|
if child == none:
|
||||||
return none;
|
return none;
|
||||||
@@ -182,7 +182,7 @@ fun parseObjectValue(ctx) {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseStringValue(ctx) {
|
fun parseString(ctx) {
|
||||||
|
|
||||||
next(ctx); # Skip the '"'.
|
next(ctx); # Skip the '"'.
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ fun parseStringValue(ctx) {
|
|||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseNumberValue(ctx) {
|
fun parseNumber(ctx) {
|
||||||
|
|
||||||
assert(isDigit(current(ctx)));
|
assert(isDigit(current(ctx)));
|
||||||
|
|
||||||
@@ -251,49 +251,50 @@ fun parseNumberValue(ctx) {
|
|||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseAnyValue(ctx) {
|
fun parseAny(ctx) {
|
||||||
|
|
||||||
|
print('Parsing value\n');
|
||||||
|
|
||||||
assert(not ended(ctx));
|
assert(not ended(ctx));
|
||||||
|
|
||||||
if current(ctx) == '[':
|
if current(ctx) == '[':
|
||||||
return parseArrayValue(ctx);
|
return parseArray(ctx);
|
||||||
|
|
||||||
if current(ctx) == '{':
|
if current(ctx) == '{':
|
||||||
return parseObjectValue(ctx);
|
return parseObject(ctx);
|
||||||
|
|
||||||
if current(ctx) == '"':
|
if current(ctx) == '"':
|
||||||
return parseStringValue(ctx);
|
return parseString(ctx);
|
||||||
|
|
||||||
if isDigit(current(ctx)):
|
if isDigit(current(ctx)):
|
||||||
return parseNumberValue(ctx);
|
return parseNumber(ctx);
|
||||||
|
|
||||||
error("Not implemented yet");
|
error("Not implemented yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
tests = [
|
#tests = ['', '1', '10', '1.10', '"jeje"', '[]', '[1,2,3]', ' [ ] ', ' [ 1 , 2 , 3 ]', '{}', ' { } ', '{"hoy":4}', ' { "hoy" : 4 } '];
|
||||||
'',
|
|
||||||
'1',
|
|
||||||
'10',
|
|
||||||
'1.10',
|
|
||||||
'"jeje"',
|
|
||||||
'[]',
|
|
||||||
'[1,2,3]',
|
|
||||||
' [ ] ',
|
|
||||||
' [ 1 , 2 , 3 ]',
|
|
||||||
'{}',
|
|
||||||
' { } ',
|
|
||||||
'{"hoy":4}',
|
|
||||||
' { "hoy" : 4 } ',
|
|
||||||
'a'
|
|
||||||
];
|
|
||||||
|
|
||||||
i = 0;
|
#i = 0;
|
||||||
while i < count(tests): {
|
#while i < count(tests): {
|
||||||
r = parse(tests[i]);
|
# r = parse(tests[i]);
|
||||||
if r == none:
|
# if r == none:
|
||||||
print('\nTest ', i, ' failed\n');
|
# print('\nTest ', i, ' failed\n');
|
||||||
else
|
# else
|
||||||
print(tests[i], ' -> ', r, '\n');
|
# print(tests[i], ' -> ', r, '\n');
|
||||||
i = i + 1;
|
# i = i + 1;
|
||||||
}
|
#}
|
||||||
|
|
||||||
|
file = files.openFile('examples/large-file.json', files.READ);
|
||||||
|
if file == none:
|
||||||
|
error("Failed to open file");
|
||||||
|
|
||||||
|
buff = newBuffer(30000000);
|
||||||
|
if buff == none:
|
||||||
|
error("Failed to create buffer");
|
||||||
|
|
||||||
|
n = files.read(file, buff);
|
||||||
|
|
||||||
|
text = bufferToString(sliceBuffer(buff, 0, n));
|
||||||
|
|
||||||
|
print(parse(text));
|
||||||
|
|||||||
@@ -211,9 +211,8 @@ static Object *bin_strcat(Runtime *runtime, Object **argv, unsigned int argc, Er
|
|||||||
for(unsigned int i = 0, written = 0; i < argc; i += 1)
|
for(unsigned int i = 0, written = 0; i < argc; i += 1)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
const char *s;
|
const char *s = Object_ToString(argv[i], &n,
|
||||||
|
Runtime_GetHeap(runtime), error);
|
||||||
s = Object_ToString(argv[i], &n, Runtime_GetHeap(runtime), error);
|
|
||||||
|
|
||||||
if(error->occurred)
|
if(error->occurred)
|
||||||
goto done;
|
goto done;
|
||||||
|
|||||||
@@ -240,8 +240,8 @@ _Bool Executable_Fetch(Executable *exe, int index, Opcode *opcode, Operand *ops,
|
|||||||
OperandType type = info->optypes[i];
|
OperandType type = info->optypes[i];
|
||||||
assert(type != OPTP_PROMISE);
|
assert(type != OPTP_PROMISE);
|
||||||
|
|
||||||
switch(type) {
|
switch(type)
|
||||||
|
{
|
||||||
case OPTP_STRING:
|
case OPTP_STRING:
|
||||||
{
|
{
|
||||||
int data_offset = instr->operands[i].as_int;
|
int data_offset = instr->operands[i].as_int;
|
||||||
@@ -457,7 +457,6 @@ _Bool ExeBuilder_Append(ExeBuilder *exeb, Error *error, Opcode opcode, Operand *
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case OPTP_PROMISE:
|
case OPTP_PROMISE:
|
||||||
|
|
||||||
assert(info->optypes[i] != OPTP_STRING);
|
assert(info->optypes[i] != OPTP_STRING);
|
||||||
|
|
||||||
// This must be incremented before subscribing
|
// This must be incremented before subscribing
|
||||||
@@ -480,7 +479,6 @@ _Bool ExeBuilder_Append(ExeBuilder *exeb, Error *error, Opcode opcode, Operand *
|
|||||||
case OPTP_FLOAT:
|
case OPTP_FLOAT:
|
||||||
instr->operands[i].as_float = opv[i].as_float;
|
instr->operands[i].as_float = opv[i].as_float;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -516,7 +516,6 @@ static _Bool emit_instr_for_node(ExeBuilder *exeb, Node *node, Promise *break_de
|
|||||||
if(!ExeBuilder_Append(exeb, error, OPCODE_POP, &op, 1, stmt->offset, 0))
|
if(!ExeBuilder_Append(exeb, error, OPCODE_POP, &op, 1, stmt->offset, 0))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt = stmt->next;
|
stmt = stmt->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-32
@@ -138,14 +138,10 @@ static Node *parse_dowhile_statement(Context *ctx);
|
|||||||
|
|
||||||
static inline _Bool isoper(char c)
|
static inline _Bool isoper(char c)
|
||||||
{
|
{
|
||||||
return c == '+' ||
|
return c == '+' || c == '-' ||
|
||||||
c == '-' ||
|
c == '*' || c == '/' ||
|
||||||
c == '*' ||
|
c == '<' || c == '>' ||
|
||||||
c == '/' ||
|
c == '!' || c == '=';
|
||||||
c == '<' ||
|
|
||||||
c == '>' ||
|
|
||||||
c == '!' ||
|
|
||||||
c == '=';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Symbol: tokenize
|
/* Symbol: tokenize
|
||||||
@@ -192,7 +188,6 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
|
|||||||
if(i < len && str[i] == '#')
|
if(i < len && str[i] == '#')
|
||||||
{
|
{
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
||||||
while(i < len && str[i] != '\n')
|
while(i < len && str[i] != '\n')
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
@@ -284,7 +279,6 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
|
|||||||
if(str[i] == '\\')
|
if(str[i] == '\\')
|
||||||
{
|
{
|
||||||
i += 1; // Consume the \.
|
i += 1; // Consume the \.
|
||||||
|
|
||||||
if(i < len && (str[i] == '\'' || str[i] == '"'))
|
if(i < len && (str[i] == '\'' || str[i] == '"'))
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
@@ -745,10 +739,8 @@ static Node *parse_string_primary_expression(Context *ctx)
|
|||||||
int segm_off, segm_len;
|
int segm_off, segm_len;
|
||||||
{
|
{
|
||||||
segm_off = i;
|
segm_off = i;
|
||||||
|
|
||||||
while(i < len && src[i] != '\\')
|
while(i < len && src[i] != '\\')
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
||||||
segm_len = i - segm_off;
|
segm_len = i - segm_off;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -928,7 +920,6 @@ static Node *parse_map_primary_expression(Context *ctx)
|
|||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
Node *key;
|
Node *key;
|
||||||
|
|
||||||
_Bool key_is_a_single_ident;
|
_Bool key_is_a_single_ident;
|
||||||
{
|
{
|
||||||
_Bool key_starts_with_an_ident = current(ctx) == TIDENT;
|
_Bool key_starts_with_an_ident = current(ctx) == TIDENT;
|
||||||
@@ -1096,6 +1087,7 @@ static Node *parse_primary_expresion(Context *ctx)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
switch(current(ctx))
|
switch(current(ctx))
|
||||||
{
|
{
|
||||||
case '[':
|
case '[':
|
||||||
@@ -1124,7 +1116,6 @@ static Node *parse_primary_expresion(Context *ctx)
|
|||||||
Error_Report(ctx->error, 0, "Missing \")\", after sub-expression");
|
Error_Report(ctx->error, 0, "Missing \")\", after sub-expression");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
next(ctx); // Consume the ')'.
|
next(ctx); // Consume the ')'.
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -1294,7 +1285,6 @@ static Node *parse_primary_expresion(Context *ctx)
|
|||||||
node->base.length = ctx->token->length;
|
node->base.length = ctx->token->length;
|
||||||
node->kind = EXPR_FALSE;
|
node->kind = EXPR_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (Node*) node;
|
return (Node*) node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1375,12 +1365,10 @@ static Node *parse_postfix_expression(Context *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Node *idx = makeStringExprNode(ctx, ctx->src + ctx->token->offset, ctx->token->length);
|
Node *idx = makeStringExprNode(ctx, ctx->src + ctx->token->offset, ctx->token->length);
|
||||||
|
|
||||||
if(idx == NULL)
|
if(idx == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
Node *sel = makeIndexSelectionExprNode(ctx, node, idx);
|
Node *sel = makeIndexSelectionExprNode(ctx, node, idx);
|
||||||
|
|
||||||
if(sel == NULL)
|
if(sel == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -1396,7 +1384,6 @@ static Node *parse_postfix_expression(Context *ctx)
|
|||||||
case '[':
|
case '[':
|
||||||
{
|
{
|
||||||
Node *idx = parse_list_primary_expression(ctx);
|
Node *idx = parse_list_primary_expression(ctx);
|
||||||
|
|
||||||
if(idx == NULL)
|
if(idx == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -1409,7 +1396,6 @@ static Node *parse_postfix_expression(Context *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Node *sel = makeIndexSelectionExprNode(ctx, node, ls->itemc == 1 ? ls->items : (Node*) ls);
|
Node *sel = makeIndexSelectionExprNode(ctx, node, ls->itemc == 1 ? ls->items : (Node*) ls);
|
||||||
|
|
||||||
if(sel == NULL)
|
if(sel == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -1485,7 +1471,6 @@ static Node *parse_postfix_expression(Context *ctx)
|
|||||||
call->argv = argv;
|
call->argv = argv;
|
||||||
call->argc = argc;
|
call->argc = argc;
|
||||||
}
|
}
|
||||||
|
|
||||||
node = (Node*) call;
|
node = (Node*) call;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1561,18 +1546,12 @@ static inline _Bool isbinop(Token *tok)
|
|||||||
{
|
{
|
||||||
assert(tok != NULL);
|
assert(tok != NULL);
|
||||||
|
|
||||||
return tok->kind == '+' ||
|
return tok->kind == '+' || tok->kind == '-' ||
|
||||||
tok->kind == '-' ||
|
tok->kind == '*' || tok->kind == '/' ||
|
||||||
tok->kind == '*' ||
|
tok->kind == '<' || tok->kind == '>' ||
|
||||||
tok->kind == '/' ||
|
tok->kind == TLEQ || tok->kind == TGEQ ||
|
||||||
tok->kind == '<' ||
|
tok->kind == TEQL || tok->kind == TNQL ||
|
||||||
tok->kind == '>' ||
|
tok->kind == TKWAND || tok->kind == TKWOR ||
|
||||||
tok->kind == TLEQ ||
|
|
||||||
tok->kind == TGEQ ||
|
|
||||||
tok->kind == TEQL ||
|
|
||||||
tok->kind == TNQL ||
|
|
||||||
tok->kind == TKWAND ||
|
|
||||||
tok->kind == TKWOR ||
|
|
||||||
tok->kind == '=';
|
tok->kind == '=';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -123,7 +123,7 @@ static _Bool interpret(Source *src)
|
|||||||
if(exe == NULL)
|
if(exe == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
Runtime *runt = Runtime_New(-1, -1, NULL, NULL);
|
Runtime *runt = Runtime_New(-1, 1024*1024, NULL, NULL);
|
||||||
|
|
||||||
if(runt == NULL)
|
if(runt == NULL)
|
||||||
{
|
{
|
||||||
@@ -313,7 +313,6 @@ int main(int argc, char **argv)
|
|||||||
Error_Free(&error);
|
Error_Free(&error);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = interpret_code(argv[3]);
|
r = interpret_code(argv[3]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
+4
-2
@@ -503,10 +503,12 @@ void Heap_CollectReference(Object **referer, void *userp)
|
|||||||
// Collect all of the references to
|
// Collect all of the references to
|
||||||
// extensions allocate using the GC'd
|
// extensions allocate using the GC'd
|
||||||
// heap.
|
// heap.
|
||||||
Object_WalkExtensions(new_location, Heap_CollectExtension, heap);
|
Object_WalkExtensions(new_location,
|
||||||
|
Heap_CollectExtension, heap);
|
||||||
|
|
||||||
// Now collect all of the children.
|
// Now collect all of the children.
|
||||||
Object_WalkReferences(new_location, Heap_CollectReference, heap);
|
Object_WalkReferences(new_location,
|
||||||
|
Heap_CollectReference, heap);
|
||||||
|
|
||||||
// Update the referer
|
// Update the referer
|
||||||
*referer = new_location;
|
*referer = new_location;
|
||||||
|
|||||||
@@ -68,6 +68,9 @@ static TypeObject t_string = {
|
|||||||
|
|
||||||
static int char_index_to_offset(StringObject *str, int idx)
|
static int char_index_to_offset(StringObject *str, int idx)
|
||||||
{
|
{
|
||||||
|
if(str->count == str->bytes)
|
||||||
|
return idx;
|
||||||
|
|
||||||
// Iterate over a string to find the first byte of
|
// Iterate over a string to find the first byte of
|
||||||
// the utf-8 character number [idx].
|
// the utf-8 character number [idx].
|
||||||
|
|
||||||
|
|||||||
@@ -59,9 +59,7 @@ static void walk(Object *self, void (*callback)(Object **referer, void *userp),
|
|||||||
|
|
||||||
static Object *call(Object *self, Object **argv, unsigned int argc, Heap *heap, Error *error)
|
static Object *call(Object *self, Object **argv, unsigned int argc, Heap *heap, Error *error)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL && heap != NULL && error != NULL);
|
||||||
assert(heap != NULL);
|
|
||||||
assert(error != NULL);
|
|
||||||
|
|
||||||
FunctionObject *func = (FunctionObject*) self;
|
FunctionObject *func = (FunctionObject*) self;
|
||||||
|
|
||||||
|
|||||||
@@ -257,7 +257,6 @@ void Snapshot_Free(Snapshot *snapshot)
|
|||||||
for(int i = 0; i < snapshot->depth; i += 1)
|
for(int i = 0; i < snapshot->depth; i += 1)
|
||||||
{
|
{
|
||||||
Executable *exe = snapshot->nodes[i].exe;
|
Executable *exe = snapshot->nodes[i].exe;
|
||||||
|
|
||||||
Executable_Free(exe);
|
Executable_Free(exe);
|
||||||
}
|
}
|
||||||
free(snapshot);
|
free(snapshot);
|
||||||
|
|||||||
Reference in New Issue
Block a user