Compare commits
1 Commits
prerelease
...
prerelease
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a19b1519f |
@@ -13,6 +13,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
const char FILE_IDENTIFIER[] = "ARBI";
|
||||||
|
const uint64_t version_number = 0;
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
if (argc <= 1)
|
if (argc <= 1)
|
||||||
@@ -52,10 +55,15 @@ int main(int argc, char *argv[]) {
|
|||||||
uint64_t constantsSize = (uint64_t)translated.constants.size;
|
uint64_t constantsSize = (uint64_t)translated.constants.size;
|
||||||
uint64_t bytecodeSize = (uint64_t)translated.bytecode.size;
|
uint64_t bytecodeSize = (uint64_t)translated.bytecode.size;
|
||||||
|
|
||||||
|
uint64_t version_number_htole64ed = htole64(version_number);
|
||||||
|
regCount = htole64(regCount);
|
||||||
regCount = htole64(regCount);
|
regCount = htole64(regCount);
|
||||||
constantsSize = htole64(constantsSize);
|
constantsSize = htole64(constantsSize);
|
||||||
bytecodeSize = htole64(bytecodeSize);
|
bytecodeSize = htole64(bytecodeSize);
|
||||||
|
|
||||||
|
|
||||||
|
fwrite(&FILE_IDENTIFIER, sizeof(char), sizeof(FILE_IDENTIFIER)/sizeof(char), file);
|
||||||
|
fwrite(&version_number_htole64ed, sizeof(uint64_t), 1, file);
|
||||||
fwrite(®Count, sizeof(uint64_t), 1, file);
|
fwrite(®Count, sizeof(uint64_t), 1, file);
|
||||||
fwrite(&constantsSize, sizeof(uint64_t), 1, file);
|
fwrite(&constantsSize, sizeof(uint64_t), 1, file);
|
||||||
fwrite(&bytecodeSize, sizeof(uint64_t), 1, file);
|
fwrite(&bytecodeSize, sizeof(uint64_t), 1, file);
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
|
|||||||
break;
|
break;
|
||||||
case TOKEN_STRING:
|
case TOKEN_STRING:
|
||||||
(*index)++;
|
(*index)++;
|
||||||
output = parse_string(file,token);
|
output = parse_string(token);
|
||||||
break;
|
break;
|
||||||
case TOKEN_NEW_LINE:
|
case TOKEN_NEW_LINE:
|
||||||
(*index)++;
|
(*index)++;
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ char *unquote_json_string(const char *input, size_t *out_len) {
|
|||||||
size_t input_len = end - (input + 1); // length inside quotes
|
size_t input_len = end - (input + 1); // length inside quotes
|
||||||
const char *src = input + 1;
|
const char *src = input + 1;
|
||||||
// Allocate max output size = input_len, decoded string cannot be longer than input_len
|
// Allocate max output size = input_len, decoded string cannot be longer than input_len
|
||||||
char *outbuf = (char *)malloc(input_len + 1);
|
char *outbuf = (char *)checked_malloc(input_len + 1);
|
||||||
if (!outbuf) return NULL;
|
if (!outbuf) return NULL;
|
||||||
|
|
||||||
char *dst = outbuf;
|
char *dst = outbuf;
|
||||||
@@ -245,7 +245,7 @@ char *unquote(char *str, size_t *decoded_len) {
|
|||||||
return unescaped;
|
return unescaped;
|
||||||
}
|
}
|
||||||
|
|
||||||
ParsedValue *parse_string(char*file,Token* token) {
|
ParsedValue *parse_string(Token* token) {
|
||||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||||
parsedValue->type = AST_STRING;
|
parsedValue->type = AST_STRING;
|
||||||
ParsedString *parsedString = checked_malloc(sizeof(ParsedString));
|
ParsedString *parsedString = checked_malloc(sizeof(ParsedString));
|
||||||
|
|||||||
@@ -15,6 +15,6 @@ char *swap_quotes(char *input, char quote);
|
|||||||
|
|
||||||
char *unquote(char *str, size_t *decoded_len);
|
char *unquote(char *str, size_t *decoded_len);
|
||||||
|
|
||||||
ParsedValue *parse_string(char*file,Token* token);
|
ParsedValue *parse_string(Token* token);
|
||||||
|
|
||||||
#endif // STRING_UTILS_H
|
#endif // STRING_UTILS_H
|
||||||
29
src/translator/declaration/declaration.c
Normal file
29
src/translator/declaration/declaration.c
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "../translator.h"
|
||||||
|
#include "declaration.h"
|
||||||
|
#include "../../parser/declaration/declaration.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
size_t translate_parsed_declaration(Translated *translated,
|
||||||
|
ParsedValue *parsedValue) {
|
||||||
|
DArray *delcarations = (DArray *)parsedValue->data;
|
||||||
|
set_registers(translated, 2);
|
||||||
|
size_t first = 0;
|
||||||
|
for (size_t i = 0; i < delcarations->size; i++) {
|
||||||
|
// TODO: add function delclaration
|
||||||
|
ParsedSingleDeclaration*singleDeclaration = darray_get(delcarations, i);
|
||||||
|
size_t temp = translate_parsed(translated, singleDeclaration->from);
|
||||||
|
if (i==0) first = temp;
|
||||||
|
size_t length = strlen(singleDeclaration->name);
|
||||||
|
size_t offset = arena_push(&translated->constants, singleDeclaration->name, length);
|
||||||
|
push_instruction_code(translated, OP_LOAD_CONST);
|
||||||
|
push_instruction_code(translated, 1);
|
||||||
|
push_instruction_code(translated, TYPE_OP_STRING);
|
||||||
|
push_instruction_code(translated,length);
|
||||||
|
push_instruction_code(translated, offset);
|
||||||
|
push_instruction_code(translated, OP_DECLARE);
|
||||||
|
push_instruction_code(translated, 0);
|
||||||
|
push_instruction_code(translated, 1);
|
||||||
|
}
|
||||||
|
return first;
|
||||||
|
}
|
||||||
10
src/translator/declaration/declaration.h
Normal file
10
src/translator/declaration/declaration.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef BYTECODE_DECLARATION_H
|
||||||
|
#define BYTECODE_DECLARATION_H
|
||||||
|
#include "../translator.h"
|
||||||
|
|
||||||
|
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue);
|
||||||
|
|
||||||
|
size_t translate_parsed_declaration(Translated *translated,
|
||||||
|
ParsedValue *parsedValue);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,18 +1,20 @@
|
|||||||
#include "../translator.h"
|
#include "../translator.h"
|
||||||
#include "../../parser/string/string.h"
|
#include "../../parser/string/string.h"
|
||||||
|
#include "string.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void translate_parsed_string(Translated *translated, ParsedValue *parsedValue) {
|
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue) {
|
||||||
ParsedString *parsedString = (ParsedString*)parsedValue->data;
|
ParsedString *parsedString = (ParsedString*)parsedValue->data;
|
||||||
size_t string_pos = arena_push(&translated->constants, parsedString->string, parsedString->length);
|
size_t string_pos = arena_push(&translated->constants, parsedString->string, parsedString->length);
|
||||||
set_registers(translated, 1);
|
set_registers(translated, 1);
|
||||||
push_instruction_code(translated, OP_LOAD_CONST);
|
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
|
||||||
push_instruction_code(translated, 0);
|
push_instruction_code(translated, 0);
|
||||||
push_instruction_code(translated, OP_TYPE_STRING);
|
push_instruction_code(translated, TYPE_OP_STRING);
|
||||||
push_instruction_code(translated,parsedString->length);
|
push_instruction_code(translated,parsedString->length);
|
||||||
push_instruction_code(translated, string_pos);
|
push_instruction_code(translated, string_pos);
|
||||||
fwrite(parsedString->string, 1, parsedString->length, stdout);
|
fwrite(parsedString->string, 1, parsedString->length, stdout);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
return start;
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef STRING_H
|
#ifndef BYTECODE_STRING_H
|
||||||
#define STRING_H
|
#define BYTECODE_STRING_H
|
||||||
#include "../translator.h"
|
#include "../translator.h"
|
||||||
|
|
||||||
void translate_parsed_string(Translated * translator, ParsedValue * parsedValue);
|
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "translator.h"
|
#include "translator.h"
|
||||||
#include "string/string.h"
|
#include "string/string.h"
|
||||||
|
#include "declaration/declaration.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -51,6 +52,12 @@ Translated init_translator() {
|
|||||||
return translated;
|
return translated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_instruction_code(Translated * translator, size_t offset, uint64_t code) {
|
||||||
|
code = htole64(code);
|
||||||
|
size_t* ptr = (translator->bytecode.data+offset);
|
||||||
|
*ptr = code;
|
||||||
|
}
|
||||||
|
|
||||||
size_t push_instruction_code(Translated * translator, uint64_t code) {
|
size_t push_instruction_code(Translated * translator, uint64_t code) {
|
||||||
code = htole64(code);
|
code = htole64(code);
|
||||||
size_t offset = translator->bytecode.size;
|
size_t offset = translator->bytecode.size;
|
||||||
@@ -62,11 +69,14 @@ void set_registers(Translated * translator, size_t count) {
|
|||||||
if (count>translator->registerCount) translator->registerCount = count;
|
if (count>translator->registerCount) translator->registerCount = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void translate_parsed(Translated * translator, ParsedValue * parsedValue) {
|
size_t translate_parsed(Translated * translator, ParsedValue * parsedValue) {
|
||||||
switch (parsedValue->type) {
|
switch (parsedValue->type) {
|
||||||
case AST_STRING:
|
case AST_STRING:
|
||||||
translate_parsed_string(translator,parsedValue);
|
return translate_parsed_string(translator,parsedValue);
|
||||||
|
case AST_DECLARATION:
|
||||||
|
return translate_parsed_declaration(translator,parsedValue);
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void translate(Translated * translator, DArray *ast) {
|
void translate(Translated * translator, DArray *ast) {
|
||||||
|
|||||||
@@ -2,14 +2,13 @@
|
|||||||
#define TRANSLATOR_H
|
#define TRANSLATOR_H
|
||||||
|
|
||||||
#include "../dynamic_array/darray.h"
|
#include "../dynamic_array/darray.h"
|
||||||
|
#include "../memory.h"
|
||||||
|
#include "../parser/parser.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "../dynamic_array/darray.h"
|
|
||||||
#include "../parser/parser.h"
|
|
||||||
#include "../memory.h"
|
|
||||||
|
|
||||||
typedef enum { OP_LOAD_CONST=255 } OperationType;
|
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL, OP_JUMP } OperationType;
|
||||||
typedef enum { OP_TYPE_STRING=255 } types;
|
typedef enum { TYPE_OP_STRING = 255 } types;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void *data;
|
void *data;
|
||||||
@@ -23,18 +22,22 @@ typedef struct {
|
|||||||
ConstantArena constants;
|
ConstantArena constants;
|
||||||
} Translated;
|
} Translated;
|
||||||
|
|
||||||
void * arena_get(ConstantArena *arena, size_t offset);
|
void *arena_get(ConstantArena *arena, size_t offset);
|
||||||
|
|
||||||
size_t arena_push(ConstantArena *arena, const void *data, size_t length);
|
size_t arena_push(ConstantArena *arena, const void *data, size_t length);
|
||||||
|
|
||||||
size_t push_instruction_code(Translated * translator, uint64_t code);
|
void set_instruction_code(Translated * translator, size_t offset, uint64_t code);
|
||||||
|
|
||||||
void set_registers(Translated * translator, size_t count);
|
size_t push_instruction_code(Translated *translator, uint64_t code);
|
||||||
|
|
||||||
|
void set_registers(Translated *translator, size_t count);
|
||||||
|
|
||||||
Translated init_translator();
|
Translated init_translator();
|
||||||
|
|
||||||
void translate(Translated * translator, DArray *ast);
|
size_t translate_parsed(Translated * translator, ParsedValue * parsedValue);
|
||||||
|
|
||||||
void free_translator(Translated * translated);
|
void translate(Translated *translator, DArray *ast);
|
||||||
|
|
||||||
|
void free_translator(Translated *translated);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
4
test.ar
4
test.ar
@@ -59,7 +59,7 @@ else term.log("bruh")
|
|||||||
|
|
||||||
mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423
|
mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423
|
||||||
|
|
||||||
x = [
|
let x = [
|
||||||
'hello world',
|
'hello world',
|
||||||
'wow',
|
'wow',
|
||||||
10
|
10
|
||||||
@@ -67,7 +67,7 @@ x = [
|
|||||||
|
|
||||||
term.log(x[0:1:1])
|
term.log(x[0:1:1])
|
||||||
|
|
||||||
y = {
|
let y = {
|
||||||
'hello':10,
|
'hello':10,
|
||||||
world:'nice'
|
world:'nice'
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user