new curly bracket coding convention

This commit is contained in:
cozis
2022-04-04 00:07:33 +02:00
parent 29e80c6319
commit 07137b50c6
28 changed files with 4036 additions and 4059 deletions
+39 -38
View File
@@ -42,10 +42,10 @@ fun parse(str) {
return none;
}
return parseAnyValue(ctx);
return parseAny(ctx);
}
fun parseArrayValue(ctx) {
fun parseArray(ctx) {
assert(current(ctx) == '[');
@@ -67,7 +67,7 @@ fun parseArrayValue(ctx) {
while true: {
child = parseAnyValue(ctx);
child = parseAny(ctx);
if child == none:
return none;
@@ -103,7 +103,7 @@ fun parseArrayValue(ctx) {
return arr;
}
fun parseObjectValue(ctx) {
fun parseObject(ctx) {
assert(current(ctx) == '{');
@@ -130,7 +130,7 @@ fun parseObjectValue(ctx) {
return none;
}
key = parseStringValue(ctx);
key = parseString(ctx);
if key == none:
return none;
@@ -146,7 +146,7 @@ fun parseObjectValue(ctx) {
skip(ctx, isSpace);
child = parseAnyValue(ctx);
child = parseAny(ctx);
if child == none:
return none;
@@ -182,7 +182,7 @@ fun parseObjectValue(ctx) {
return obj;
}
fun parseStringValue(ctx) {
fun parseString(ctx) {
next(ctx); # Skip the '"'.
@@ -207,7 +207,7 @@ fun parseStringValue(ctx) {
return buff;
}
fun parseNumberValue(ctx) {
fun parseNumber(ctx) {
assert(isDigit(current(ctx)));
@@ -251,49 +251,50 @@ fun parseNumberValue(ctx) {
return parsed;
}
fun parseAnyValue(ctx) {
fun parseAny(ctx) {
print('Parsing value\n');
assert(not ended(ctx));
if current(ctx) == '[':
return parseArrayValue(ctx);
return parseArray(ctx);
if current(ctx) == '{':
return parseObjectValue(ctx);
return parseObject(ctx);
if current(ctx) == '"':
return parseStringValue(ctx);
return parseString(ctx);
if isDigit(current(ctx)):
return parseNumberValue(ctx);
return parseNumber(ctx);
error("Not implemented yet");
}
tests = [
'',
'1',
'10',
'1.10',
'"jeje"',
'[]',
'[1,2,3]',
' [ ] ',
' [ 1 , 2 , 3 ]',
'{}',
' { } ',
'{"hoy":4}',
' { "hoy" : 4 } ',
'a'
];
#tests = ['', '1', '10', '1.10', '"jeje"', '[]', '[1,2,3]', ' [ ] ', ' [ 1 , 2 , 3 ]', '{}', ' { } ', '{"hoy":4}', ' { "hoy" : 4 } '];
i = 0;
while i < count(tests): {
r = parse(tests[i]);
if r == none:
print('\nTest ', i, ' failed\n');
else
print(tests[i], ' -> ', r, '\n');
i = i + 1;
}
#i = 0;
#while i < count(tests): {
# r = parse(tests[i]);
# if r == none:
# print('\nTest ', i, ' failed\n');
# else
# print(tests[i], ' -> ', r, '\n');
# 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));
+2 -3
View File
@@ -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)
{
int n;
const char *s;
s = Object_ToString(argv[i], &n, Runtime_GetHeap(runtime), error);
const char *s = Object_ToString(argv[i], &n,
Runtime_GetHeap(runtime), error);
if(error->occurred)
goto done;
+2 -4
View File
@@ -240,8 +240,8 @@ _Bool Executable_Fetch(Executable *exe, int index, Opcode *opcode, Operand *ops,
OperandType type = info->optypes[i];
assert(type != OPTP_PROMISE);
switch(type) {
switch(type)
{
case OPTP_STRING:
{
int data_offset = instr->operands[i].as_int;
@@ -457,7 +457,6 @@ _Bool ExeBuilder_Append(ExeBuilder *exeb, Error *error, Opcode opcode, Operand *
break;
case OPTP_PROMISE:
assert(info->optypes[i] != OPTP_STRING);
// This must be incremented before subscribing
@@ -480,7 +479,6 @@ _Bool ExeBuilder_Append(ExeBuilder *exeb, Error *error, Opcode opcode, Operand *
case OPTP_FLOAT:
instr->operands[i].as_float = opv[i].as_float;
break;
}
}
}
-1
View File
@@ -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))
return 0;
}
stmt = stmt->next;
}
+11 -32
View File
@@ -138,14 +138,10 @@ static Node *parse_dowhile_statement(Context *ctx);
static inline _Bool isoper(char c)
{
return c == '+' ||
c == '-' ||
c == '*' ||
c == '/' ||
c == '<' ||
c == '>' ||
c == '!' ||
c == '=';
return c == '+' || c == '-' ||
c == '*' || c == '/' ||
c == '<' || c == '>' ||
c == '!' || c == '=';
}
/* Symbol: tokenize
@@ -192,7 +188,6 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
if(i < len && str[i] == '#')
{
i += 1;
while(i < len && str[i] != '\n')
i += 1;
}
@@ -284,7 +279,6 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
if(str[i] == '\\')
{
i += 1; // Consume the \.
if(i < len && (str[i] == '\'' || str[i] == '"'))
i += 1;
}
@@ -745,10 +739,8 @@ static Node *parse_string_primary_expression(Context *ctx)
int segm_off, segm_len;
{
segm_off = i;
while(i < len && src[i] != '\\')
i += 1;
segm_len = i - segm_off;
}
@@ -928,7 +920,6 @@ static Node *parse_map_primary_expression(Context *ctx)
while(1)
{
Node *key;
_Bool key_is_a_single_ident;
{
_Bool key_starts_with_an_ident = current(ctx) == TIDENT;
@@ -1096,6 +1087,7 @@ static Node *parse_primary_expresion(Context *ctx)
return NULL;
}
switch(current(ctx))
{
case '[':
@@ -1124,7 +1116,6 @@ static Node *parse_primary_expresion(Context *ctx)
Error_Report(ctx->error, 0, "Missing \")\", after sub-expression");
return NULL;
}
next(ctx); // Consume the ')'.
return node;
}
@@ -1294,7 +1285,6 @@ static Node *parse_primary_expresion(Context *ctx)
node->base.length = ctx->token->length;
node->kind = EXPR_FALSE;
}
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);
if(idx == NULL)
return NULL;
Node *sel = makeIndexSelectionExprNode(ctx, node, idx);
if(sel == NULL)
return NULL;
@@ -1396,7 +1384,6 @@ static Node *parse_postfix_expression(Context *ctx)
case '[':
{
Node *idx = parse_list_primary_expression(ctx);
if(idx == 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);
if(sel == NULL)
return NULL;
@@ -1485,7 +1471,6 @@ static Node *parse_postfix_expression(Context *ctx)
call->argv = argv;
call->argc = argc;
}
node = (Node*) call;
break;
}
@@ -1561,18 +1546,12 @@ static inline _Bool isbinop(Token *tok)
{
assert(tok != NULL);
return tok->kind == '+' ||
tok->kind == '-' ||
tok->kind == '*' ||
tok->kind == '/' ||
tok->kind == '<' ||
tok->kind == '>' ||
tok->kind == TLEQ ||
tok->kind == TGEQ ||
tok->kind == TEQL ||
tok->kind == TNQL ||
tok->kind == TKWAND ||
tok->kind == TKWOR ||
return tok->kind == '+' || tok->kind == '-' ||
tok->kind == '*' || tok->kind == '/' ||
tok->kind == '<' || tok->kind == '>' ||
tok->kind == TLEQ || tok->kind == TGEQ ||
tok->kind == TEQL || tok->kind == TNQL ||
tok->kind == TKWAND || tok->kind == TKWOR ||
tok->kind == '=';
}
+1 -2
View File
@@ -123,7 +123,7 @@ static _Bool interpret(Source *src)
if(exe == NULL)
return 0;
Runtime *runt = Runtime_New(-1, -1, NULL, NULL);
Runtime *runt = Runtime_New(-1, 1024*1024, NULL, NULL);
if(runt == NULL)
{
@@ -313,7 +313,6 @@ int main(int argc, char **argv)
Error_Free(&error);
return -1;
}
r = interpret_code(argv[3]);
}
else
+4 -2
View File
@@ -503,10 +503,12 @@ void Heap_CollectReference(Object **referer, void *userp)
// Collect all of the references to
// extensions allocate using the GC'd
// heap.
Object_WalkExtensions(new_location, Heap_CollectExtension, heap);
Object_WalkExtensions(new_location,
Heap_CollectExtension, heap);
// Now collect all of the children.
Object_WalkReferences(new_location, Heap_CollectReference, heap);
Object_WalkReferences(new_location,
Heap_CollectReference, heap);
// Update the referer
*referer = new_location;
+3
View File
@@ -68,6 +68,9 @@ static TypeObject t_string = {
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
// the utf-8 character number [idx].
+1 -3
View File
@@ -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)
{
assert(self != NULL);
assert(heap != NULL);
assert(error != NULL);
assert(self != NULL && heap != NULL && error != NULL);
FunctionObject *func = (FunctionObject*) self;
-1
View File
@@ -257,7 +257,6 @@ void Snapshot_Free(Snapshot *snapshot)
for(int i = 0; i < snapshot->depth; i += 1)
{
Executable *exe = snapshot->nodes[i].exe;
Executable_Free(exe);
}
free(snapshot);