Compare commits
18 Commits
prerelease
...
prerelease
| Author | SHA1 | Date | |
|---|---|---|---|
| 72cc87f5b6 | |||
| 5c0ced5e45 | |||
| 886599c9c5 | |||
| cbebe4812b | |||
| 8b2eedf589 | |||
| 48647f3734 | |||
| 47379a2621 | |||
| 246e20014f | |||
| c31f16d68d | |||
| 49ae0223cb | |||
| d868de4ab9 | |||
| 3adecb4eba | |||
| 31f38d8ba4 | |||
| aa65393e2c | |||
| 358127a145 | |||
| 6828cc5f1a | |||
| a9d0ba0318 | |||
| a275a0a0ad |
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@@ -12,6 +12,8 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Setup Python (needed for Conan)
|
||||
uses: actions/setup-python@v4
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -61,3 +61,5 @@ build
|
||||
*.yy.h
|
||||
|
||||
out.arbin
|
||||
rand_test.ar
|
||||
__arcache__
|
||||
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
[submodule "external/xxhash"]
|
||||
path = external/xxhash
|
||||
url = https://github.com/Cyan4973/xxHash.git
|
||||
[submodule "external/cwalk"]
|
||||
path = external/cwalk
|
||||
url = https://github.com/likle/cwalk.git
|
||||
@@ -30,7 +30,9 @@ add_custom_command(
|
||||
add_custom_target(GenerateLexer DEPENDS ${LEXER_C} ${LEXER_H})
|
||||
|
||||
# Step 3: Add executable
|
||||
add_executable(argon ${CFILES} ${LEXER_C})
|
||||
add_executable(argon external/xxhash/xxhash.c external/cwalk/src/cwalk.c ${CFILES} ${LEXER_C})
|
||||
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
|
||||
target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/cwalk/include)
|
||||
|
||||
# Step 4: Build order
|
||||
add_dependencies(argon GenerateLexer)
|
||||
@@ -56,3 +58,5 @@ target_link_libraries(argon PRIVATE
|
||||
target_include_directories(argon PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/lexer
|
||||
)
|
||||
|
||||
add_custom_command(TARGET argon POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:argon>)
|
||||
4
LICENSE
4
LICENSE
@@ -1,3 +1,7 @@
|
||||
This project is licensed under the GNU General Public License version 3 (GPLv3), except for
|
||||
some files (e.g., in the `src/hash_data/siphash/` directory) which are licensed under CC0 1.0 Universal.
|
||||
See the LICENSE-CC0 file in the respective directories for details.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
|
||||
10
Makefile
10
Makefile
@@ -2,8 +2,8 @@ LEXER_SRC = src/lexer/lex.l
|
||||
LEXER_C = src/lexer/lex.yy.c
|
||||
LEXER_H = src/lexer/lex.yy.h
|
||||
|
||||
CFILES = $(shell find src -name '*.c')
|
||||
CFLAGS = $(ARCHFLAGS) -lm -lgc -lgmp -Wall -Wextra -Wno-unused-function
|
||||
CFILES = external/xxhash/xxhash.c external/cwalk/src/cwalk.c $(shell find src -name '*.c')
|
||||
CFLAGS = $(ARCHFLAGS) -lm -lgc -lgmp -Wall -Wextra -Wno-unused-function -Iexternal/cwalk/include
|
||||
BINARY = bin/argon
|
||||
|
||||
all: $(BINARY)
|
||||
@@ -15,6 +15,10 @@ $(BINARY): $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||
mkdir -p bin
|
||||
gcc -O3 -o $(BINARY) $(CFILES) $(CFLAGS) -s
|
||||
|
||||
native: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||
mkdir -p bin
|
||||
gcc -O3 -march=native -o $(BINARY) $(CFILES) $(CFLAGS)
|
||||
|
||||
debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||
mkdir -p bin
|
||||
gcc -g -O0 -o $(BINARY) $(CFILES) $(CFLAGS)
|
||||
@@ -26,7 +30,7 @@ full-debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||
optimised: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||
mkdir -p bin
|
||||
gcc -O3 -fprofile-generate -o $(BINARY) $(CFILES) $(CFLAGS)
|
||||
${BINARY} test.ar
|
||||
${BINARY} rand_test.ar
|
||||
gcc -O3 -fprofile-use -o $(BINARY) $(CFILES) $(CFLAGS)
|
||||
|
||||
|
||||
|
||||
1
external/cwalk
vendored
Submodule
1
external/cwalk
vendored
Submodule
Submodule external/cwalk added at e98d23f688
1
external/xxhash
vendored
Submodule
1
external/xxhash
vendored
Submodule
Submodule external/xxhash added at 38d555879f
7
gentest.py
Normal file
7
gentest.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import random
|
||||
myfile = open("rand_test.ar","w")
|
||||
|
||||
for i in range(10000000):
|
||||
myfile.write("\"")
|
||||
myfile.write(str(random.random())[2::])
|
||||
myfile.write("\"\n")
|
||||
@@ -1,2 +0,0 @@
|
||||
null
|
||||
"hello\u0000world"
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
#define CHUNK_SIZE 1024
|
||||
#define CHUNK_SIZE 1048576
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
|
||||
28
src/hash_data/hash_data.c
Normal file
28
src/hash_data/hash_data.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include "siphash/siphash.h"
|
||||
#include "hash_data.h"
|
||||
|
||||
uint8_t siphash_key[16];
|
||||
uint8_t empty_siphash_key[16];
|
||||
|
||||
void generate_siphash_key(uint8_t hash_key[16]) {
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd < 0 || read(fd, hash_key, 16) != 16) {
|
||||
// Fallback or abort
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
uint64_t siphash64_bytes(const void *data, size_t len,uint8_t hash_key[16]) {
|
||||
uint8_t out[8];
|
||||
if (siphash(data, len, hash_key, out, sizeof(out)) != 0)
|
||||
return 0;
|
||||
|
||||
uint64_t hash = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
hash |= ((uint64_t)out[i]) << (8 * i);
|
||||
|
||||
return hash;
|
||||
}
|
||||
11
src/hash_data/hash_data.h
Normal file
11
src/hash_data/hash_data.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef HASH_DATA_H
|
||||
#define HASH_DATA_H
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint8_t siphash_key[16];
|
||||
extern uint8_t empty_siphash_key[16];
|
||||
|
||||
void generate_siphash_key(uint8_t siphash_key[16]);
|
||||
uint64_t siphash64_bytes(const void *data, size_t len,uint8_t siphash_key[16]);
|
||||
#endif //HASH_DATA_H
|
||||
122
src/hash_data/siphash/LICENSE_CC0
Normal file
122
src/hash_data/siphash/LICENSE_CC0
Normal file
@@ -0,0 +1,122 @@
|
||||
This license applies **only** to the files that explicitly state at the top of the file that they are under CC0 1.0 Universal.
|
||||
|
||||
All other files in this project are licensed under the GNU General Public License version 3 (GPLv3) unless otherwise stated.
|
||||
|
||||
Please refer to the LICENSE file in the root directory for the main project license.
|
||||
|
||||
CC0 1.0 Universal
|
||||
|
||||
Statement of Purpose
|
||||
|
||||
The laws of most jurisdictions throughout the world automatically confer
|
||||
exclusive Copyright and Related Rights (defined below) upon the creator and
|
||||
subsequent owner(s) (each and all, an "owner") of an original work of
|
||||
authorship and/or a database (each, a "Work").
|
||||
|
||||
Certain owners wish to permanently relinquish those rights to a Work for the
|
||||
purpose of contributing to a commons of creative, cultural and scientific
|
||||
works ("Commons") that the public can reliably and without fear of later
|
||||
claims of infringement build upon, modify, incorporate in other works, reuse
|
||||
and redistribute as freely as possible in any form whatsoever and for any
|
||||
purposes, including without limitation commercial purposes. These owners may
|
||||
contribute to the Commons to promote the ideal of a free culture and the
|
||||
further production of creative, cultural and scientific works, or to gain
|
||||
reputation or greater distribution for their Work in part through the use and
|
||||
efforts of others.
|
||||
|
||||
For these and/or other purposes and motivations, and without any expectation
|
||||
of additional consideration or compensation, the person associating CC0 with a
|
||||
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
||||
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
||||
and publicly distribute the Work under its terms, with knowledge of his or her
|
||||
Copyright and Related Rights in the Work and the meaning and intended legal
|
||||
effect of CC0 on those rights.
|
||||
|
||||
1. Copyright and Related Rights. A Work made available under CC0 may be
|
||||
protected by copyright and related or neighboring rights ("Copyright and
|
||||
Related Rights"). Copyright and Related Rights include, but are not limited
|
||||
to, the following:
|
||||
|
||||
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
||||
and translate a Work;
|
||||
|
||||
ii. moral rights retained by the original author(s) and/or performer(s);
|
||||
|
||||
iii. publicity and privacy rights pertaining to a person's image or likeness
|
||||
depicted in a Work;
|
||||
|
||||
iv. rights protecting against unfair competition in regards to a Work,
|
||||
subject to the limitations in paragraph 4(a), below;
|
||||
|
||||
v. rights protecting the extraction, dissemination, use and reuse of data in
|
||||
a Work;
|
||||
|
||||
vi. database rights (such as those arising under Directive 96/9/EC of the
|
||||
European Parliament and of the Council of 11 March 1996 on the legal
|
||||
protection of databases, and under any national implementation thereof,
|
||||
including any amended or successor version of such directive); and
|
||||
|
||||
vii. other similar, equivalent or corresponding rights throughout the world
|
||||
based on applicable law or treaty, and any national implementations thereof.
|
||||
|
||||
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
||||
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
||||
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
||||
and Related Rights and associated claims and causes of action, whether now
|
||||
known or unknown (including existing as well as future claims and causes of
|
||||
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
||||
duration provided by applicable law or treaty (including future time
|
||||
extensions), (iii) in any current or future medium and for any number of
|
||||
copies, and (iv) for any purpose whatsoever, including without limitation
|
||||
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
||||
the Waiver for the benefit of each member of the public at large and to the
|
||||
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
||||
shall not be subject to revocation, rescission, cancellation, termination, or
|
||||
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
||||
by the public as contemplated by Affirmer's express Statement of Purpose.
|
||||
|
||||
3. Public License Fallback. Should any part of the Waiver for any reason be
|
||||
judged legally invalid or ineffective under applicable law, then the Waiver
|
||||
shall be preserved to the maximum extent permitted taking into account
|
||||
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
||||
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
||||
non transferable, non sublicensable, non exclusive, irrevocable and
|
||||
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
||||
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
||||
provided by applicable law or treaty (including future time extensions), (iii)
|
||||
in any current or future medium and for any number of copies, and (iv) for any
|
||||
purpose whatsoever, including without limitation commercial, advertising or
|
||||
promotional purposes (the "License"). The License shall be deemed effective as
|
||||
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
||||
License for any reason be judged legally invalid or ineffective under
|
||||
applicable law, such partial invalidity or ineffectiveness shall not
|
||||
invalidate the remainder of the License, and in such case Affirmer hereby
|
||||
affirms that he or she will not (i) exercise any of his or her remaining
|
||||
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
||||
and causes of action with respect to the Work, in either case contrary to
|
||||
Affirmer's express Statement of Purpose.
|
||||
|
||||
4. Limitations and Disclaimers.
|
||||
|
||||
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
||||
surrendered, licensed or otherwise affected by this document.
|
||||
|
||||
b. Affirmer offers the Work as-is and makes no representations or warranties
|
||||
of any kind concerning the Work, express, implied, statutory or otherwise,
|
||||
including without limitation warranties of title, merchantability, fitness
|
||||
for a particular purpose, non infringement, or the absence of latent or
|
||||
other defects, accuracy, or the present or absence of errors, whether or not
|
||||
discoverable, all to the greatest extent permissible under applicable law.
|
||||
|
||||
c. Affirmer disclaims responsibility for clearing rights of other persons
|
||||
that may apply to the Work or any use thereof, including without limitation
|
||||
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
||||
disclaims responsibility for obtaining any necessary consents, permissions
|
||||
or other rights required for any use of the Work.
|
||||
|
||||
d. Affirmer understands and acknowledges that Creative Commons is not a
|
||||
party to this document and has no duty or obligation with respect to this
|
||||
CC0 or use of the Work.
|
||||
|
||||
For more information, please see
|
||||
<http://creativecommons.org/publicdomain/zero/1.0/>
|
||||
@@ -1,18 +1,22 @@
|
||||
/*
|
||||
SipHash reference C implementation
|
||||
|
||||
Copyright (c) 2012-2022 Jean-Philippe Aumasson
|
||||
<jeanphilippe.aumasson@gmail.com>
|
||||
Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
|
||||
|
||||
To the extent possible under law, the author(s) have dedicated all copyright
|
||||
and related and neighboring rights to this software to the public domain
|
||||
worldwide. This software is distributed without any warranty.
|
||||
|
||||
You should have received a copy of the CC0 Public Domain Dedication along
|
||||
with
|
||||
this software. If not, see
|
||||
<http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
* This file is dedicated to the public domain under CC0 1.0 Universal.
|
||||
* See the LICENSE-CC0 file in this directory for details.
|
||||
*
|
||||
* SipHash reference C implementation
|
||||
*
|
||||
* Copyright (c) 2012-2022 Jean-Philippe Aumasson
|
||||
* <jeanphilippe.aumasson@gmail.com>
|
||||
* Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
|
||||
*
|
||||
* To the extent possible under law, the author(s) have dedicated all copyright
|
||||
* and related and neighboring rights to this software to the public domain
|
||||
* worldwide. This software is distributed without any warranty.
|
||||
*
|
||||
* You should have received a copy of the CC0 Public Domain Dedication along
|
||||
* with this software. If not, see
|
||||
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*/
|
||||
|
||||
#include "siphash.h"
|
||||
26
src/hash_data/siphash/siphash.h
Normal file
26
src/hash_data/siphash/siphash.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* This file is dedicated to the public domain under CC0 1.0 Universal.
|
||||
* See the LICENSE-CC0 file in this directory for details.
|
||||
*
|
||||
* SipHash reference C implementation
|
||||
*
|
||||
* Copyright (c) 2012-2022 Jean-Philippe Aumasson
|
||||
* <jeanphilippe.aumasson@gmail.com>
|
||||
* Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
|
||||
*
|
||||
* To the extent possible under law, the author(s) have dedicated all copyright
|
||||
* and related and neighboring rights to this software to the public domain
|
||||
* worldwide. This software is distributed without any warranty.
|
||||
*
|
||||
* You should have received a copy of the CC0 Public Domain Dedication along
|
||||
* with this software. If not, see
|
||||
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
|
||||
const size_t outlen);
|
||||
134
src/hashmap/hashmap.c
Normal file
134
src/hashmap/hashmap.c
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "hashmap.h"
|
||||
#include "../memory.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct hashmap *createHashmap() {
|
||||
size_t size = 8;
|
||||
struct hashmap *t = (struct hashmap *)checked_malloc(sizeof(struct hashmap));
|
||||
t->size = size;
|
||||
t->order = 1;
|
||||
t->list = (struct node **)checked_malloc(sizeof(struct node *) * size);
|
||||
memset(t->list, 0, sizeof(struct node *) * size);
|
||||
return t;
|
||||
}
|
||||
|
||||
void hashmap_free(struct hashmap *t, free_val_func free_val) {
|
||||
if (!t)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < t->size; i++) {
|
||||
struct node *current = t->list[i];
|
||||
while (current) {
|
||||
struct node *next = current->next;
|
||||
if (free_val && current->val) {
|
||||
free_val(current->val);
|
||||
}
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
free(t->list);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void resize_hashmap(struct hashmap *t) {
|
||||
int old_size = t->size;
|
||||
int new_size = old_size * 2;
|
||||
|
||||
struct node **old_list = t->list;
|
||||
|
||||
// Create new list
|
||||
t->list = (struct node **)checked_malloc(sizeof(struct node *) * new_size);
|
||||
memset(t->list, 0, sizeof(struct node *) * new_size);
|
||||
|
||||
t->size = new_size;
|
||||
t->count = 0;
|
||||
|
||||
// Rehash old entries into new list
|
||||
for (int i = 0; i < old_size; i++) {
|
||||
struct node *temp = old_list[i];
|
||||
while (temp) {
|
||||
hashmap_insert(t, temp->hash, temp->key, temp->val,
|
||||
temp->order); // Will increment count
|
||||
struct node *temp_temp = temp;
|
||||
temp = temp->next;
|
||||
free(temp_temp);
|
||||
}
|
||||
}
|
||||
free(old_list);
|
||||
}
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash) { return hash & (t->size - 1); }
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
struct node *prev = NULL;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
t->list[pos] = temp->next;
|
||||
return 1;
|
||||
}
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
list = NULL;
|
||||
prev = NULL;
|
||||
temp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key, void *val,
|
||||
size_t order) {
|
||||
if (!order) {
|
||||
order = t->order++;
|
||||
}
|
||||
if ((t->count + 1) > t->size * 0.75) {
|
||||
resize_hashmap(t);
|
||||
}
|
||||
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
|
||||
// Check if key exists → overwrite
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
temp->val = val;
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// Insert new node
|
||||
struct node *newNode = (struct node *)checked_malloc(sizeof(struct node));
|
||||
newNode->hash = hash;
|
||||
newNode->key = key;
|
||||
newNode->val = val;
|
||||
newNode->order = order;
|
||||
newNode->next = list;
|
||||
t->list[pos] = newNode;
|
||||
t->count++;
|
||||
}
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
return temp->val;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
37
src/hashmap/hashmap.h
Normal file
37
src/hashmap/hashmap.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef HASHMAP_H
|
||||
#define HASHMAP_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
|
||||
typedef void (*free_val_func)(void *val);
|
||||
|
||||
struct node {
|
||||
uint64_t hash;
|
||||
void *key;
|
||||
void *val;
|
||||
size_t order;
|
||||
struct node *next;
|
||||
};
|
||||
struct hashmap {
|
||||
size_t size;
|
||||
size_t count;
|
||||
size_t order;
|
||||
struct node **list;
|
||||
};
|
||||
|
||||
struct hashmap *createHashmap();
|
||||
|
||||
void hashmap_free(struct hashmap *t, free_val_func free_val);
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash);
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash);
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
void *val, size_t order);
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash);
|
||||
|
||||
#endif // HASHMAP_H
|
||||
279
src/main.c
279
src/main.c
@@ -3,69 +3,261 @@
|
||||
#include "lexer/token.h"
|
||||
#include "memory.h"
|
||||
#include "parser/parser.h"
|
||||
#include "translator/translator.h"
|
||||
#include "runtime/runtime.h"
|
||||
#include "translator/translator.h"
|
||||
|
||||
#include "../external/xxhash/xxhash.h"
|
||||
#include "hash_data/hash_data.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <endian.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#define mkdir(path, mode) _mkdir(path)
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#include "../external/cwalk/include/cwalk.h"
|
||||
#include <string.h>
|
||||
|
||||
const char FILE_IDENTIFIER[] = "ARBI";
|
||||
const uint64_t version_number = 0;
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
setlocale(LC_ALL, "");
|
||||
if (argc <= 1)
|
||||
return -1;
|
||||
ar_memory_init();
|
||||
char *path = argv[1];
|
||||
DArray tokens;
|
||||
char *get_current_directory() {
|
||||
char *buffer = NULL;
|
||||
|
||||
darray_init(&tokens, sizeof(Token));
|
||||
#ifdef _WIN32
|
||||
DWORD size = GetCurrentDirectoryA(0, NULL);
|
||||
buffer = malloc(size);
|
||||
if (buffer == NULL)
|
||||
return NULL;
|
||||
if (GetCurrentDirectoryA(size, buffer) == 0) {
|
||||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
#else
|
||||
long size = pathconf(".", _PC_PATH_MAX);
|
||||
if (size == -1)
|
||||
size = 4096; // fallback
|
||||
buffer = malloc(size);
|
||||
if (buffer == NULL)
|
||||
return NULL;
|
||||
if (getcwd(buffer, size) == NULL) {
|
||||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
if (!file) {
|
||||
return -1;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
LexerState state = {path, file, 0, 0, &tokens};
|
||||
int ensure_dir_exists(const char *path) {
|
||||
struct stat st = {0};
|
||||
|
||||
if (stat(path, &st) == -1) {
|
||||
// Directory does not exist, create it
|
||||
if (mkdir(path, 0755) != 0) {
|
||||
perror("mkdir failed");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char CACHE_FOLDER[] = "__arcache__";
|
||||
const char FILE_IDENTIFIER[5] = "ARBI";
|
||||
const char BYTECODE_EXTENTION[] = "arbin";
|
||||
const uint32_t version_number = 0;
|
||||
|
||||
int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash) {
|
||||
FILE *bytecode_file = fopen(joined_paths, "rb");
|
||||
if (!bytecode_file)
|
||||
return 1;
|
||||
char file_identifier_from_cache[sizeof(FILE_IDENTIFIER)] = {0};
|
||||
if (fread(&file_identifier_from_cache, 1,
|
||||
sizeof(file_identifier_from_cache) - 1,
|
||||
bytecode_file) != sizeof(file_identifier_from_cache) - 1 ||
|
||||
memcmp(file_identifier_from_cache, FILE_IDENTIFIER,
|
||||
sizeof(file_identifier_from_cache)) != 0) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
uint32_t read_version;
|
||||
if (fread(&read_version, 1, sizeof(read_version), bytecode_file) !=
|
||||
sizeof(read_version)) {
|
||||
goto FAILED;
|
||||
}
|
||||
read_version = le32toh(read_version);
|
||||
|
||||
if (read_version != version_number) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
uint64_t read_hash;
|
||||
if (fread(&read_hash, 1, sizeof(read_hash), bytecode_file) !=
|
||||
sizeof(read_hash)) {
|
||||
goto FAILED;
|
||||
}
|
||||
read_hash = le64toh(read_hash);
|
||||
|
||||
if (read_hash != hash) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
uint8_t register_count;
|
||||
if (fread(®ister_count, 1, sizeof(register_count), bytecode_file) !=
|
||||
sizeof(register_count)) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
uint64_t constantsSize;
|
||||
if (fread(&constantsSize, 1, sizeof(constantsSize), bytecode_file) !=
|
||||
sizeof(constantsSize)) {
|
||||
goto FAILED;
|
||||
}
|
||||
constantsSize = le64toh(constantsSize);
|
||||
|
||||
uint64_t bytecodeSize;
|
||||
if (fread(&bytecodeSize, 1, sizeof(bytecodeSize), bytecode_file) !=
|
||||
sizeof(bytecodeSize)) {
|
||||
goto FAILED;
|
||||
}
|
||||
bytecodeSize = le64toh(bytecodeSize);
|
||||
|
||||
arena_resize(&translated_dest->constants, constantsSize);
|
||||
|
||||
if (fread(translated_dest->constants.data, 1, constantsSize, bytecode_file) !=
|
||||
constantsSize) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
darray_resize(&translated_dest->bytecode, bytecodeSize);
|
||||
|
||||
if (fread(translated_dest->bytecode.data, 1, bytecodeSize, bytecode_file) !=
|
||||
bytecodeSize) {
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
translated_dest->registerCount = register_count;
|
||||
|
||||
fclose(bytecode_file);
|
||||
return 0;
|
||||
FAILED:
|
||||
fclose(bytecode_file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ArgonObject *execute(char*absolute_path) {
|
||||
clock_t start, end;
|
||||
double time_spent, total_time_spent = 0;
|
||||
|
||||
const char *basename_ptr;
|
||||
size_t basename_length;
|
||||
cwk_path_get_basename(absolute_path, &basename_ptr, &basename_length);
|
||||
|
||||
if (!basename_ptr) return NULL;
|
||||
|
||||
char basename[FILENAME_MAX];
|
||||
memcpy(basename, basename_ptr, basename_length);
|
||||
|
||||
size_t parent_directory_length;
|
||||
cwk_path_get_dirname(absolute_path, &parent_directory_length);
|
||||
|
||||
char parent_directory[FILENAME_MAX];
|
||||
memcpy(parent_directory, absolute_path, parent_directory_length);
|
||||
parent_directory[parent_directory_length] = '\0';
|
||||
|
||||
char cache_folder_path[FILENAME_MAX];
|
||||
cwk_path_join(parent_directory, CACHE_FOLDER, cache_folder_path, sizeof(cache_folder_path));
|
||||
|
||||
char cache_file_path[FILENAME_MAX];
|
||||
cwk_path_join(cache_folder_path, basename, cache_file_path, sizeof(cache_file_path));
|
||||
cwk_path_change_extension(cache_file_path, BYTECODE_EXTENTION, cache_file_path, sizeof(cache_file_path));
|
||||
|
||||
FILE *file = fopen(absolute_path, "r");
|
||||
if (!file) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
XXH3_state_t *hash_state = XXH3_createState();
|
||||
XXH3_64bits_reset(hash_state);
|
||||
|
||||
char buffer[8192];
|
||||
size_t bytes;
|
||||
while ((bytes = fread(buffer, 1, sizeof(buffer), file)) > 0) {
|
||||
XXH3_64bits_update(hash_state, buffer, bytes);
|
||||
}
|
||||
rewind(file);
|
||||
uint64_t hash = XXH3_64bits_digest(hash_state);
|
||||
XXH3_freeState(hash_state);
|
||||
|
||||
Translated translated = init_translator();
|
||||
|
||||
if (load_cache(&translated, cache_file_path, hash) != 0) {
|
||||
free_translator(&translated);
|
||||
translated = init_translator();
|
||||
|
||||
DArray tokens;
|
||||
darray_init(&tokens, sizeof(Token));
|
||||
|
||||
LexerState state = {absolute_path, file, 0, 0, &tokens};
|
||||
start = clock();
|
||||
lexer(state);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
total_time_spent += time_spent;
|
||||
printf("Lexer time taken: %f seconds\n", time_spent);
|
||||
fclose(state.file);
|
||||
|
||||
DArray ast;
|
||||
|
||||
darray_init(&ast, sizeof(ParsedValue));
|
||||
|
||||
parser(path, &ast, &tokens, false);
|
||||
start = clock();
|
||||
parser(absolute_path, &ast, &tokens, false);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
total_time_spent += time_spent;
|
||||
printf("Parser time taken: %f seconds\n", time_spent);
|
||||
darray_free(&tokens, free_token);
|
||||
|
||||
Translated translated = init_translator();
|
||||
|
||||
start = clock();
|
||||
translate(&translated, &ast);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
total_time_spent += time_spent;
|
||||
printf("Translation time taken: %f seconds\n", time_spent);
|
||||
|
||||
darray_free(&ast, free_parsed);
|
||||
ensure_dir_exists(cache_folder_path);
|
||||
|
||||
file = fopen("out.arbin", "wb");
|
||||
file = fopen(cache_file_path, "wb");
|
||||
|
||||
uint64_t regCount = (uint64_t)translated.registerCount;
|
||||
uint64_t constantsSize = (uint64_t)translated.constants.size;
|
||||
uint64_t bytecodeSize = (uint64_t)translated.bytecode.size;
|
||||
|
||||
uint64_t version_number_htole64ed = htole64(version_number);
|
||||
regCount = htole64(regCount);
|
||||
regCount = htole64(regCount);
|
||||
uint32_t version_number_htole32ed = htole32(version_number);
|
||||
uint64_t net_hash = htole64(hash);
|
||||
constantsSize = htole64(constantsSize);
|
||||
bytecodeSize = htole64(bytecodeSize);
|
||||
|
||||
fwrite(&FILE_IDENTIFIER, sizeof(char), strlen(FILE_IDENTIFIER), file);
|
||||
fwrite(&version_number_htole64ed, sizeof(uint64_t), 1, file);
|
||||
fwrite(®Count, sizeof(uint64_t), 1, file);
|
||||
fwrite(&version_number_htole32ed, sizeof(uint32_t), 1, file);
|
||||
fwrite(&net_hash, sizeof(net_hash), 1, file);
|
||||
fwrite(&translated.registerCount, sizeof(uint8_t), 1, file);
|
||||
fwrite(&constantsSize, sizeof(uint64_t), 1, file);
|
||||
fwrite(&bytecodeSize, sizeof(uint64_t), 1, file);
|
||||
fwrite(translated.constants.data, 1, translated.constants.size, file);
|
||||
@@ -73,13 +265,36 @@ int main(int argc, char *argv[]) {
|
||||
translated.bytecode.size, file);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
generate_siphash_key();
|
||||
start = clock();
|
||||
RuntimeState state = init_runtime_state(translated);
|
||||
ArgonObject *resp = runtime(translated,state);
|
||||
|
||||
init_types();
|
||||
|
||||
runtime(translated);
|
||||
end = clock();
|
||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
total_time_spent += time_spent;
|
||||
printf("Execution time taken: %f seconds\n", time_spent);
|
||||
printf("total time taken: %f seconds\n", total_time_spent);
|
||||
|
||||
free_translator(&translated);
|
||||
return 0;
|
||||
return resp;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
setlocale(LC_ALL, "");
|
||||
ar_memory_init();
|
||||
generate_siphash_key(siphash_key);
|
||||
init_types();
|
||||
char *CWD = get_current_directory();
|
||||
if (argc <= 1)
|
||||
return -1;
|
||||
char *path_non_absolute = argv[1];
|
||||
char path[FILENAME_MAX];
|
||||
cwk_path_get_absolute(CWD, path_non_absolute, path,
|
||||
sizeof(path));
|
||||
free(CWD);
|
||||
ArgonObject *resp = execute(path);
|
||||
if (resp) return 0;
|
||||
return -1;
|
||||
}
|
||||
13
src/memory.c
13
src/memory.c
@@ -1,13 +1,9 @@
|
||||
#include "memory.h"
|
||||
#include <gc.h>
|
||||
#include <gc/gc.h>
|
||||
#include <gmp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // for malloc/free (temp arena fallback)
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// runtime
|
||||
#include "runtime/objects/type/type.h"
|
||||
|
||||
void *checked_malloc(size_t size) {
|
||||
void *ptr = malloc(size);
|
||||
@@ -23,7 +19,6 @@ void *gmp_gc_realloc(void *ptr, size_t old_size, size_t new_size) {
|
||||
return GC_realloc(ptr, new_size);
|
||||
}
|
||||
|
||||
|
||||
void gmp_gc_free(void *ptr, size_t size) {
|
||||
(void)size; // Boehm GC manages this itself
|
||||
// No-op — memory will be collected automatically
|
||||
@@ -35,9 +30,13 @@ void ar_memory_init() {
|
||||
mp_set_memory_functions(GC_malloc, gmp_gc_realloc, gmp_gc_free);
|
||||
}
|
||||
|
||||
|
||||
void *ar_alloc(size_t size) { return GC_MALLOC(size); }
|
||||
|
||||
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
|
||||
GC_finalization_proc *old_fn, void **old_client_data) {
|
||||
return GC_register_finalizer_no_order(obj, fn, client_data, old_fn, old_client_data);
|
||||
}
|
||||
|
||||
void *ar_alloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
|
||||
|
||||
char *ar_strdup(const char *str) {
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
#define ARGON_MEMORY_H
|
||||
|
||||
#include <stddef.h> // for size_t
|
||||
#include <gc/gc.h>
|
||||
|
||||
// GC-managed allocations
|
||||
|
||||
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
|
||||
GC_finalization_proc *old_fn, void **old_client_data);
|
||||
void *ar_alloc(size_t size);
|
||||
void *ar_alloc_atomic(size_t size);
|
||||
char *ar_strdup(const char *str);
|
||||
|
||||
@@ -22,7 +22,9 @@ ParsedValue *parse_access(char *file, DArray *tokens, size_t *index,
|
||||
ParsedValue *parsedString = parse_string(token, false);
|
||||
darray_push(&parsedAccess->access, parsedString);
|
||||
free(parsedString);
|
||||
parsedAccess->access_fields = true;
|
||||
} else {
|
||||
parsedAccess->access_fields = false;
|
||||
while (true) {
|
||||
skip_newlines_and_indents(tokens, index);
|
||||
error_if_finished(file, tokens, index);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
typedef struct {
|
||||
ParsedValue to_access;
|
||||
bool access_fields;
|
||||
DArray access;
|
||||
} ParsedAccess;
|
||||
|
||||
|
||||
@@ -49,5 +49,6 @@ void free_parse_call(void *ptr) {
|
||||
|
||||
darray_free(&parsedCall->args, free_parsed);
|
||||
free_parsed(parsedCall->to_call);
|
||||
free(parsedCall->to_call);
|
||||
free(parsedCall);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "declaration.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../../memory.h"
|
||||
#include "../function/function.h"
|
||||
#include "../literals/literals.h"
|
||||
#include "../parser.h"
|
||||
#include <stdio.h>
|
||||
@@ -22,7 +23,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
darray_push(declarations, &_declaration);
|
||||
ParsedSingleDeclaration *declaration =
|
||||
darray_get(declarations, declarations->size - 1);
|
||||
declaration->is_function = false;
|
||||
bool isFunction=false;
|
||||
DArray parameters;
|
||||
declaration->from = parse_null();
|
||||
|
||||
if (token->type != TOKEN_IDENTIFIER) {
|
||||
@@ -38,8 +40,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
return parsedValue;
|
||||
token = darray_get(tokens, *index);
|
||||
if (token->type == TOKEN_LPAREN) {
|
||||
declaration->is_function = true;
|
||||
darray_init(&declaration->parameters, sizeof(char *));
|
||||
isFunction = true;
|
||||
darray_init(¶meters, sizeof(char *));
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
token = darray_get(tokens, *index);
|
||||
@@ -61,9 +63,9 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
file, token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
char *parameter_name =
|
||||
strcpy(checked_malloc(strlen(token->value) + 1), token->value);
|
||||
darray_push(&declaration->parameters, ¶meter_name);
|
||||
char *parameter_name = checked_malloc(strlen(token->value) + 1);
|
||||
strcpy(parameter_name, token->value);
|
||||
darray_push(¶meters, ¶meter_name);
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
skip_newlines_and_indents(tokens, index);
|
||||
@@ -97,10 +99,15 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if (isFunction) {
|
||||
declaration->from = create_parsed_function(declaration->name, parameters,
|
||||
declaration->from);
|
||||
}
|
||||
if ((*index) >= tokens->size)
|
||||
break;
|
||||
token = darray_get(tokens, *index);
|
||||
}
|
||||
|
||||
size_t count = skip_newlines_and_indents(tokens, index);
|
||||
if ((*index) >= tokens->size)
|
||||
break;
|
||||
@@ -127,8 +134,6 @@ void free_string(void *ptr) {
|
||||
void free_single_declaration(void *ptr) {
|
||||
ParsedSingleDeclaration *declaration = ptr;
|
||||
free(declaration->name);
|
||||
if (declaration->is_function)
|
||||
darray_free(&declaration->parameters, free_string);
|
||||
free_parsed(declaration->from);
|
||||
free(declaration->from);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
typedef struct {
|
||||
char * name;
|
||||
bool is_function;
|
||||
DArray parameters; // string[]
|
||||
ParsedValue * from;
|
||||
} ParsedSingleDeclaration;
|
||||
|
||||
|
||||
@@ -88,5 +88,5 @@ void free_parsed_dictionary(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
DArray *parsed_dictionary = parsedValue->data;
|
||||
darray_free(parsed_dictionary, free_dictionary_entry);
|
||||
free(parsedValue->data);
|
||||
free(parsed_dictionary);
|
||||
}
|
||||
32
src/parser/function/function.c
Normal file
32
src/parser/function/function.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "function.h"
|
||||
#include "../../memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *create_parsed_function(char *name, DArray parameters,
|
||||
ParsedValue *body) {
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type=AST_FUNCTION;
|
||||
ParsedFunction *parsedFunction = checked_malloc(sizeof(ParsedFunction));
|
||||
parsedValue->data=parsedFunction;
|
||||
parsedFunction->name=strcpy(checked_malloc(strlen(name) + 1), name);
|
||||
parsedFunction->body = body;
|
||||
parsedFunction->parameters=parameters;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
void free_parameter(void *ptr) {
|
||||
char** data = ptr;
|
||||
free(*data);
|
||||
}
|
||||
|
||||
void free_function(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
ParsedFunction *parsed = parsedValue->data;
|
||||
free_parsed(parsed->body);
|
||||
free(parsed->body);
|
||||
free(parsed->name);
|
||||
darray_free(&parsed->parameters, free_parameter);
|
||||
free(parsed);
|
||||
}
|
||||
17
src/parser/function/function.h
Normal file
17
src/parser/function/function.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef FUNCTION_H
|
||||
#define FUNCTION_H
|
||||
#include "../../lexer/token.h" // for Token
|
||||
#include "../parser.h"
|
||||
|
||||
typedef struct {
|
||||
char * name;
|
||||
DArray parameters;
|
||||
ParsedValue *body;
|
||||
} ParsedFunction;
|
||||
|
||||
ParsedValue *create_parsed_function(char *name, DArray parameters,
|
||||
ParsedValue *body);
|
||||
|
||||
void free_function(void *ptr);
|
||||
|
||||
#endif // FUNCTION_H
|
||||
@@ -115,5 +115,5 @@ void free_parsed_if(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
DArray *parsed_if = parsedValue->data;
|
||||
darray_free(parsed_if, free_conditional);
|
||||
free(parsedValue->data);
|
||||
free(parsed_if);
|
||||
}
|
||||
@@ -45,5 +45,5 @@ void free_parsed_list(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
DArray *parsed_list = parsedValue->data;
|
||||
darray_free(parsed_list, free_parsed);
|
||||
free(parsedValue->data);
|
||||
free(parsed_list);
|
||||
}
|
||||
@@ -6,9 +6,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
|
||||
ParsedValue convert_to_operation(DArray *to_operate_on, DArray *operations) {
|
||||
if (to_operate_on->size == 1) {
|
||||
return darray_get(to_operate_on, 0);
|
||||
return *((ParsedValue*)darray_get(to_operate_on, 0));
|
||||
}
|
||||
TokenType operation = 0;
|
||||
DArray positions;
|
||||
@@ -23,10 +23,10 @@ ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
|
||||
}
|
||||
darray_push(&positions, &i);
|
||||
}
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_OPERATION;
|
||||
ParsedValue parsedValue;
|
||||
parsedValue.type = AST_OPERATION;
|
||||
ParsedOperation *operationStruct = checked_malloc(sizeof(ParsedOperation));
|
||||
parsedValue->data = operationStruct;
|
||||
parsedValue.data = operationStruct;
|
||||
operationStruct->operation = operation;
|
||||
darray_init(&operationStruct->to_operate_on, sizeof(ParsedValue));
|
||||
size_t last_position = 0;
|
||||
@@ -37,16 +37,18 @@ ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
|
||||
to_operate_on, to_operate_on_last_position, (*position) + 1);
|
||||
DArray operations_slice =
|
||||
darray_slice(operations, last_position, *position);
|
||||
ParsedValue result = convert_to_operation(&to_operate_on_slice, &operations_slice);
|
||||
darray_push(&operationStruct->to_operate_on,
|
||||
convert_to_operation(&to_operate_on_slice, &operations_slice));
|
||||
&result);
|
||||
last_position = (*position);
|
||||
to_operate_on_last_position = (*position) + 1;
|
||||
}
|
||||
DArray to_operate_on_slice =
|
||||
darray_slice(to_operate_on, to_operate_on_last_position, to_operate_on->size);
|
||||
DArray operations_slice = darray_slice(operations, last_position, operations->size);
|
||||
ParsedValue result =convert_to_operation(&to_operate_on_slice, &operations_slice);
|
||||
darray_push(&operationStruct->to_operate_on,
|
||||
convert_to_operation(&to_operate_on_slice, &operations_slice));
|
||||
&result);
|
||||
darray_free(&positions, NULL);
|
||||
return parsedValue;
|
||||
}
|
||||
@@ -80,10 +82,12 @@ ParsedValue *parse_operations(char *file, DArray *tokens, size_t *index,
|
||||
darray_push(&to_operate_on, parsedValue);
|
||||
free(parsedValue);
|
||||
}
|
||||
ParsedValue *output = convert_to_operation(&to_operate_on, &operations);
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
ParsedValue output = convert_to_operation(&to_operate_on, &operations);
|
||||
memcpy(parsedValue, &output,sizeof(ParsedValue));
|
||||
darray_free(&to_operate_on, NULL);
|
||||
darray_free(&operations, NULL);
|
||||
return output;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
void free_operation(void *ptr) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "declaration/declaration.h"
|
||||
#include "dictionary/dictionary.h"
|
||||
#include "dowrap/dowrap.h"
|
||||
#include "function/function.h"
|
||||
#include "if/if.h"
|
||||
#include "list/list.h"
|
||||
#include "literals/literals.h"
|
||||
@@ -24,7 +25,7 @@
|
||||
const char *ValueTypeNames[] = {
|
||||
"string", "assign", "identifier", "number", "if statement",
|
||||
"access", "call", "declaration", "null", "boolean",
|
||||
"do wrap", "operations", "list", "dictionary"};
|
||||
"do wrap", "operations", "list", "dictionary", "function"};
|
||||
|
||||
void error_if_finished(char *file, DArray *tokens, size_t *index) {
|
||||
if ((*index) >= tokens->size) {
|
||||
@@ -232,5 +233,7 @@ void free_parsed(void *ptr) {
|
||||
case AST_DICTIONARY:
|
||||
free_parsed_dictionary(parsed);
|
||||
break;
|
||||
case AST_FUNCTION:
|
||||
free_function(parsed);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,8 @@ typedef enum {
|
||||
AST_DOWRAP,
|
||||
AST_OPERATION,
|
||||
AST_LIST,
|
||||
AST_DICTIONARY
|
||||
AST_DICTIONARY,
|
||||
AST_FUNCTION
|
||||
} ValueType;
|
||||
|
||||
extern const char* ValueTypeNames[];
|
||||
|
||||
113
src/runtime/internals/dynamic_array_armem/darray_armem.c
Normal file
113
src/runtime/internals/dynamic_array_armem/darray_armem.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "darray_armem.h"
|
||||
#include "../../../memory.h"
|
||||
#include <gc/gc.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void darray_armem_init(darray_armem *arr, size_t element_size) {
|
||||
arr->element_size = element_size;
|
||||
arr->size = 0;
|
||||
arr->capacity = CHUNK_SIZE;
|
||||
arr->data = ar_alloc(CHUNK_SIZE * element_size);
|
||||
arr->resizable = true;
|
||||
if (!arr->data) {
|
||||
fprintf(stderr, "darray_armem_init: allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void darray_armem_resize(darray_armem *arr, size_t new_size) {
|
||||
if (!arr->resizable) {
|
||||
fprintf(stderr, "darray_armem_resize: unresizable darray_armem\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
|
||||
if (new_capacity != arr->capacity) {
|
||||
void *new_data = ar_alloc(new_capacity * arr->element_size);
|
||||
memccpy(new_data,arr->data, arr->element_size, arr->capacity);
|
||||
if (!new_data) {
|
||||
fprintf(stderr, "darray_armem_resize: reallocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
arr->data = new_data;
|
||||
arr->capacity = new_capacity;
|
||||
}
|
||||
|
||||
arr->size = new_size;
|
||||
}
|
||||
|
||||
void darray_armem_push(darray_armem *arr, void *element) {
|
||||
if (!arr->resizable) {
|
||||
fprintf(stderr, "darray_armem_resize: unresizable darray_armem\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (arr->size >= arr->capacity) {
|
||||
darray_armem_resize(arr, arr->size + 1);
|
||||
} else {
|
||||
arr->size++;
|
||||
}
|
||||
|
||||
void *target = (char *)arr->data + (arr->size - 1) * arr->element_size;
|
||||
memcpy(target, element, arr->element_size);
|
||||
}
|
||||
|
||||
void darray_armem_pop(darray_armem *arr, void (*free_data)(void *)) {
|
||||
if (!arr->resizable) {
|
||||
fprintf(stderr, "darray_armem_resize: unresizable darray_armem\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (arr->size == 0)
|
||||
return;
|
||||
|
||||
if (free_data) {
|
||||
void *target = (char *)arr->data + (arr->size-1) * arr->element_size;
|
||||
free_data(target);
|
||||
}
|
||||
|
||||
darray_armem_resize(arr, arr->size-1);
|
||||
}
|
||||
|
||||
void *darray_armem_get(darray_armem *arr, size_t index) {
|
||||
if (index >= arr->size) {
|
||||
fprintf(stderr, "darray_armem_get: index out of bounds\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return (char *)arr->data + index * arr->element_size;
|
||||
}
|
||||
|
||||
darray_armem darray_armem_slice(darray_armem *arr, size_t start, size_t end) {
|
||||
if (start > end || end > arr->size) {
|
||||
fprintf(stderr, "darray_armem_slice: invalid slice range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
darray_armem slice;
|
||||
|
||||
slice.data = (char *)arr->data + start * arr->element_size;
|
||||
slice.size = (end - start);
|
||||
slice.element_size = arr->element_size;
|
||||
slice.capacity = ((slice.size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
|
||||
slice.resizable = false;
|
||||
|
||||
return slice;
|
||||
}
|
||||
|
||||
void darray_armem_free(darray_armem *arr, void (*free_data)(void *)) {
|
||||
if (!arr->resizable) {
|
||||
// It's a view/slice — don't free
|
||||
return;
|
||||
}
|
||||
if (free_data) {
|
||||
for (size_t i = 0; i < arr->size; ++i) {
|
||||
void *element = (char *)arr->data + i * arr->element_size;
|
||||
free_data(element);
|
||||
}
|
||||
}
|
||||
free(arr->data);
|
||||
arr->data = NULL;
|
||||
arr->size = 0;
|
||||
arr->capacity = 0;
|
||||
arr->element_size = 0;
|
||||
arr->resizable = false;
|
||||
}
|
||||
37
src/runtime/internals/dynamic_array_armem/darray_armem.h
Normal file
37
src/runtime/internals/dynamic_array_armem/darray_armem.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef darray_armem_H
|
||||
#define darray_armem_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
#define CHUNK_SIZE 1048576
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
size_t element_size;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
bool resizable;
|
||||
} darray_armem;
|
||||
|
||||
// Initializes the dynamic_array
|
||||
void darray_armem_init(darray_armem *arr, size_t element_size);
|
||||
|
||||
// Pushes an element onto the array
|
||||
void darray_armem_push(darray_armem *arr, void *element);
|
||||
|
||||
// Pops the last element, calling `free_data` if provided
|
||||
void darray_armem_pop(darray_armem *arr, void (*free_data)(void *));
|
||||
|
||||
// Gets a pointer to an element at index
|
||||
void *darray_armem_get(darray_armem *arr, size_t index);
|
||||
|
||||
// Frees the entire array and optionally each element
|
||||
void darray_armem_free(darray_armem *arr, void (*free_data)(void *));
|
||||
|
||||
// Resizes the array to a new size (internal use, but exposed)
|
||||
void darray_armem_resize(darray_armem *arr, size_t new_size);
|
||||
|
||||
darray_armem darray_armem_slice(darray_armem *arr, size_t start, size_t end);
|
||||
|
||||
#endif // darray_armem_H
|
||||
@@ -7,47 +7,47 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct hashmap *createHashmap() {
|
||||
struct hashmap_GC *createHashmap_GC() {
|
||||
size_t size = 8;
|
||||
struct hashmap *t = (struct hashmap *)ar_alloc(sizeof(struct hashmap));
|
||||
struct hashmap_GC *t = (struct hashmap_GC *)ar_alloc(sizeof(struct hashmap_GC));
|
||||
t->size = size;
|
||||
t->order = 1;
|
||||
t->list = (struct node **)ar_alloc(sizeof(struct node *) * size);
|
||||
memset(t->list, 0, sizeof(struct node *) * size);
|
||||
t->list = (struct node_GC **)ar_alloc(sizeof(struct node_GC *) * size);
|
||||
memset(t->list, 0, sizeof(struct node_GC *) * size);
|
||||
return t;
|
||||
}
|
||||
|
||||
void resize_hashmap(struct hashmap *t) {
|
||||
void resize_hashmap_GC(struct hashmap_GC *t) {
|
||||
int old_size = t->size;
|
||||
int new_size = old_size * 2;
|
||||
|
||||
struct node **old_list = t->list;
|
||||
struct node_GC **old_list = t->list;
|
||||
|
||||
// Create new list
|
||||
t->list = (struct node **)ar_alloc(sizeof(struct node *) * new_size);
|
||||
memset(t->list, 0, sizeof(struct node *) * new_size);
|
||||
t->list = (struct node_GC **)ar_alloc(sizeof(struct node_GC *) * new_size);
|
||||
memset(t->list, 0, sizeof(struct node_GC *) * new_size);
|
||||
|
||||
t->size = new_size;
|
||||
t->count = 0;
|
||||
|
||||
// Rehash old entries into new list
|
||||
for (int i = 0; i < old_size; i++) {
|
||||
struct node *temp = old_list[i];
|
||||
struct node_GC *temp = old_list[i];
|
||||
while (temp) {
|
||||
hashmap_insert(t, temp->hash, temp->key, temp->val,
|
||||
hashmap_insert_GC(t, temp->hash, temp->key, temp->val,
|
||||
temp->order); // Will increment count
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash) { return hash % t->size; }
|
||||
int hashCode_GC(struct hashmap_GC *t, uint64_t hash) { return hash % t->size; }
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
struct node *prev = NULL;
|
||||
int hashmap_remove_GC(struct hashmap_GC *t, uint64_t hash) {
|
||||
int pos = hashCode_GC(t, hash);
|
||||
struct node_GC *list = t->list[pos];
|
||||
struct node_GC *temp = list;
|
||||
struct node_GC *prev = NULL;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
if (prev)
|
||||
@@ -65,18 +65,18 @@ int hashmap_remove(struct hashmap *t, uint64_t hash) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
void hashmap_insert_GC(struct hashmap_GC *t, uint64_t hash, void *key,
|
||||
void *val, size_t order) {
|
||||
if (!order) {
|
||||
order = t->order++;
|
||||
}
|
||||
if ((t->count + 1) > t->size * 0.75) {
|
||||
resize_hashmap(t);
|
||||
resize_hashmap_GC(t);
|
||||
}
|
||||
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
int pos = hashCode_GC(t, hash);
|
||||
struct node_GC *list = t->list[pos];
|
||||
struct node_GC *temp = list;
|
||||
|
||||
// Check if key exists → overwrite
|
||||
while (temp) {
|
||||
@@ -88,7 +88,7 @@ void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
}
|
||||
|
||||
// Insert new node
|
||||
struct node *newNode = (struct node *)ar_alloc(sizeof(struct node));
|
||||
struct node_GC *newNode = (struct node_GC *)ar_alloc(sizeof(struct node_GC));
|
||||
newNode->hash = hash;
|
||||
newNode->key = key;
|
||||
newNode->val = val;
|
||||
@@ -98,10 +98,10 @@ void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
t->count++;
|
||||
}
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash) {
|
||||
int pos = hashCode(t, hash);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
void *hashmap_lookup_GC(struct hashmap_GC *t, uint64_t hash) {
|
||||
int pos = hashCode_GC(t, hash);
|
||||
struct node_GC *list = t->list[pos];
|
||||
struct node_GC *temp = list;
|
||||
while (temp) {
|
||||
if (temp->hash == hash) {
|
||||
return temp->val;
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
#ifndef HASHMAP_H
|
||||
#define HASHMAP_H
|
||||
#ifndef HASHMAP_GC_H
|
||||
#define HASHMAP_GC_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
|
||||
struct node {
|
||||
struct node_GC {
|
||||
uint64_t hash;
|
||||
void *key;
|
||||
void *val;
|
||||
size_t order;
|
||||
struct node *next;
|
||||
struct node_GC *next;
|
||||
};
|
||||
struct hashmap {
|
||||
struct hashmap_GC {
|
||||
size_t size;
|
||||
size_t count;
|
||||
size_t order;
|
||||
struct node **list;
|
||||
struct node_GC **list;
|
||||
};
|
||||
|
||||
struct hashmap *createHashmap();
|
||||
struct hashmap_GC *createHashmap_GC();
|
||||
|
||||
int hashCode(struct hashmap *t, uint64_t hash);
|
||||
int hashCode_GC(struct hashmap_GC *t, uint64_t hash);
|
||||
|
||||
int hashmap_remove(struct hashmap *t, uint64_t hash);
|
||||
int hashmap_remove_GC(struct hashmap_GC *t, uint64_t hash);
|
||||
|
||||
void hashmap_insert(struct hashmap *t, uint64_t hash, void *key,
|
||||
void hashmap_insert_GC(struct hashmap_GC *t, uint64_t hash, void *key,
|
||||
void *val, size_t order);
|
||||
|
||||
void *hashmap_lookup(struct hashmap *t, uint64_t hash);
|
||||
void *hashmap_lookup_GC(struct hashmap_GC *t, uint64_t hash);
|
||||
|
||||
#endif // HASHMAP_H
|
||||
#endif // HASHMAP_GC_H
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
SipHash reference C implementation
|
||||
|
||||
Copyright (c) 2012-2021 Jean-Philippe Aumasson
|
||||
<jeanphilippe.aumasson@gmail.com>
|
||||
Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
|
||||
|
||||
To the extent possible under law, the author(s) have dedicated all copyright
|
||||
and related and neighboring rights to this software to the public domain
|
||||
worldwide. This software is distributed without any warranty.
|
||||
|
||||
You should have received a copy of the CC0 Public Domain Dedication along
|
||||
with
|
||||
this software. If not, see
|
||||
<http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
|
||||
const size_t outlen);
|
||||
40
src/runtime/objects/functions/functions.c
Normal file
40
src/runtime/objects/functions/functions.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "../../runtime.h"
|
||||
#include "../object.h"
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
ArgonObject *ARGON_FUNCTION_TYPE = NULL;
|
||||
|
||||
void init_function_type() {
|
||||
ARGON_FUNCTION_TYPE = init_argon_class("Function");
|
||||
}
|
||||
|
||||
void load_argon_function(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
ArgonObject *object = init_child_argon_object(ARGON_FUNCTION_TYPE);
|
||||
object->type = TYPE_FUNCTION;
|
||||
uint64_t offset = pop_bytecode(translated, state);
|
||||
uint64_t length = pop_bytecode(translated, state);
|
||||
object->name = ar_alloc_atomic(length + 1);
|
||||
memcpy(object->name, arena_get(&translated->constants, offset), length);
|
||||
object->name[length] = '\0';
|
||||
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
|
||||
object->value.argon_fn.parameters =
|
||||
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(char *));
|
||||
for (size_t i = 0; i < object->value.argon_fn.number_of_parameters; i++) {
|
||||
offset = pop_bytecode(translated, state);
|
||||
length = pop_bytecode(translated, state);
|
||||
object->value.argon_fn.parameters[i] = ar_alloc_atomic(length + 1);
|
||||
memcpy(object->value.argon_fn.parameters[i], arena_get(&translated->constants, offset), length);
|
||||
object->value.argon_fn.parameters[i][length] = '\0';
|
||||
}
|
||||
offset = pop_bytecode(translated, state);
|
||||
length = pop_bytecode(translated, state);
|
||||
darray_armem_init(&object->value.argon_fn.bytecode, sizeof(uint64_t));
|
||||
darray_armem_resize(&object->value.argon_fn.bytecode, length/object->value.argon_fn.bytecode.element_size);
|
||||
memcpy(object->value.argon_fn.bytecode.data, arena_get(&translated->constants, offset), length);
|
||||
object->value.argon_fn.stack = stack;
|
||||
state->registers[0]=object;
|
||||
}
|
||||
9
src/runtime/objects/functions/functions.h
Normal file
9
src/runtime/objects/functions/functions.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef FUNCTION_H
|
||||
#define FUNCTION_H
|
||||
#include "../object.h"
|
||||
|
||||
void init_function_type();
|
||||
|
||||
ArgonObject *load_argon_function(Translated *translated, RuntimeState *state, struct Stack stack);
|
||||
|
||||
#endif // FUNCTION_H
|
||||
@@ -3,8 +3,12 @@
|
||||
#include <string.h>
|
||||
#include "null.h"
|
||||
|
||||
ArgonObject *ARGON_NULL_TYPE = NULL;
|
||||
ArgonObject *ARGON_NULL = NULL;
|
||||
|
||||
void init_null() {
|
||||
ARGON_NULL = init_argon_object();
|
||||
ARGON_NULL_TYPE = init_argon_class("NULL_TYPE");
|
||||
|
||||
ARGON_NULL = init_child_argon_object(ARGON_NULL_TYPE);
|
||||
ARGON_NULL->type=TYPE_NULL;
|
||||
}
|
||||
@@ -1,24 +1,37 @@
|
||||
#include "object.h"
|
||||
#include "../../hash_data/hash_data.h"
|
||||
#include "../../memory.h"
|
||||
#include "../runtime.h"
|
||||
#include "type/type.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
ArgonObject *BASE_OBJECT = NULL;
|
||||
ArgonObject *BASE_CLASS = NULL;
|
||||
|
||||
void init_base_field() {
|
||||
add_field(BASE_OBJECT, "test", BASE_OBJECT);
|
||||
// add_field(BASE_CLASS, "test", BASE_CLASS);
|
||||
}
|
||||
|
||||
ArgonObject* init_argon_object() {
|
||||
ArgonObject *init_child_argon_object(ArgonObject *cls) {
|
||||
ArgonObject *object = init_argon_class(NULL);
|
||||
object->self = object;
|
||||
object->baseObject = cls;
|
||||
add_field(object, "__call__", NULL);
|
||||
return object;
|
||||
}
|
||||
|
||||
ArgonObject *init_argon_class(char *name) {
|
||||
ArgonObject *object = ar_alloc(sizeof(ArgonObject));
|
||||
object->name = name;
|
||||
object->type = TYPE_OBJECT;
|
||||
object->typeObject = NULL;
|
||||
object->baseObject = BASE_OBJECT;
|
||||
object->fields = createHashmap();
|
||||
object->self = NULL;
|
||||
object->baseObject = ARGON_TYPE;
|
||||
object->fields = createHashmap_GC();
|
||||
memset(&object->value, 0, sizeof(object->value));
|
||||
return object;
|
||||
}
|
||||
|
||||
void add_field(ArgonObject *target, char *name, ArgonObject *object) {
|
||||
hashmap_insert(target->fields, siphash64_bytes(name, strlen(name)),name, object, 0);
|
||||
hashmap_insert_GC(target->fields,
|
||||
siphash64_bytes(name, strlen(name), siphash_key), name,
|
||||
object, 0);
|
||||
}
|
||||
@@ -1,15 +1,23 @@
|
||||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
#include "../internals/hashmap/hashmap.h"
|
||||
#include "../internals/dynamic_array_armem/darray_armem.h"
|
||||
#include <gmp.h>
|
||||
#include <stdbool.h>
|
||||
#include "../runtime.h"
|
||||
|
||||
extern ArgonObject *BASE_OBJECT;
|
||||
extern ArgonObject *BASE_CLASS;
|
||||
|
||||
struct string_struct {
|
||||
char *data;
|
||||
size_t length;
|
||||
};
|
||||
struct argon_function_struct {
|
||||
darray_armem bytecode;
|
||||
struct Stack stack;
|
||||
size_t number_of_parameters;
|
||||
char** parameters;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TYPE_NULL,
|
||||
@@ -23,21 +31,23 @@ typedef enum {
|
||||
|
||||
struct ArgonObject {
|
||||
ArgonType type;
|
||||
char* name;
|
||||
ArgonObject *self;
|
||||
ArgonObject *baseObject;
|
||||
ArgonObject *typeObject;
|
||||
struct hashmap *fields; // dynamic fields/methods
|
||||
struct hashmap_GC *fields; // dynamic fields/methods
|
||||
union {
|
||||
mpq_t as_number;
|
||||
bool as_bool;
|
||||
struct string_struct as_str;
|
||||
void *native_fn;
|
||||
struct argon_function_struct argon_fn;
|
||||
// others as needed
|
||||
} value;
|
||||
};
|
||||
typedef struct ArgonObject ArgonObject;
|
||||
void init_base_field();
|
||||
|
||||
ArgonObject *init_argon_object();
|
||||
ArgonObject* init_child_argon_object(ArgonObject *cls);
|
||||
ArgonObject* init_argon_class(char*name);
|
||||
|
||||
void add_field(ArgonObject*target, char* name, ArgonObject *object);
|
||||
#endif // OBJECT_H
|
||||
@@ -2,23 +2,18 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include "string.h"
|
||||
|
||||
ArgonObject *ARGON_STRING_TYPE = NULL;
|
||||
ArgonObject *ARGON_STRING_BASE = NULL;
|
||||
|
||||
void init_string_type() {
|
||||
ARGON_STRING_TYPE = init_argon_object();
|
||||
|
||||
ARGON_STRING_BASE = init_argon_object();
|
||||
ARGON_STRING_TYPE = init_argon_class("String");
|
||||
|
||||
}
|
||||
|
||||
ArgonObject *init_string_object(char*data, size_t length) {
|
||||
fwrite(data, 1, length, stdout);
|
||||
printf("\n");
|
||||
ArgonObject * object = init_argon_object();
|
||||
object->typeObject = ARGON_STRING_TYPE;
|
||||
object->baseObject = ARGON_STRING_BASE;
|
||||
ArgonObject * object = init_child_argon_object(ARGON_STRING_TYPE);
|
||||
object->type = TYPE_STRING;
|
||||
object->value.as_str.data = data;
|
||||
object->value.as_str.length = length;
|
||||
return object;
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
ArgonObject *ARGON_TYPE = NULL;
|
||||
|
||||
void init_type() {
|
||||
ARGON_TYPE = init_argon_object();
|
||||
ARGON_TYPE = init_argon_class("type");
|
||||
ARGON_TYPE->baseObject = BASE_CLASS;
|
||||
}
|
||||
@@ -1,39 +1,58 @@
|
||||
#include "runtime.h"
|
||||
#include "../translator/translator.h"
|
||||
#include "internals/siphash/siphash.h"
|
||||
#include "objects/functions/functions.h"
|
||||
#include "objects/null/null.h"
|
||||
#include "objects/object.h"
|
||||
#include "objects/string/string.h"
|
||||
#include "objects/type/type.h"
|
||||
#include <fcntl.h>
|
||||
#include <gc/gc.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
uint64_t bytes_to_uint64(const uint8_t bytes[8]) {
|
||||
uint64_t value = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
value |= ((uint64_t)bytes[i]) << (i * 8);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void init_types() {
|
||||
BASE_OBJECT = init_argon_object();
|
||||
BASE_CLASS = init_argon_class("BASE_CLASS");
|
||||
|
||||
init_type();
|
||||
init_function_type();
|
||||
init_null();
|
||||
init_string_type();
|
||||
|
||||
init_base_field();
|
||||
}
|
||||
|
||||
|
||||
uint8_t pop_byte(Translated *translated, RuntimeState *state) {
|
||||
return *((uint8_t *)darray_get(&translated->bytecode, state->head++));
|
||||
}
|
||||
|
||||
uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
|
||||
uint64_t *instruction = darray_get(&translated->bytecode, state->head++);
|
||||
return *instruction;
|
||||
uint8_t bytes[8];
|
||||
for (size_t i = 0; i < sizeof(bytes); i++) {
|
||||
bytes[i] = *((uint8_t *)darray_get(&translated->bytecode, state->head++));
|
||||
}
|
||||
return bytes_to_uint64(bytes);
|
||||
}
|
||||
|
||||
void load_const(Translated *translated, RuntimeState *state) {
|
||||
uint64_t to_register = pop_bytecode(translated, state);
|
||||
types type = pop_bytecode(translated, state);
|
||||
uint64_t to_register = pop_byte(translated, state);
|
||||
types type = pop_byte(translated, state);
|
||||
size_t length = pop_bytecode(translated, state);
|
||||
uint64_t offset = pop_bytecode(translated, state);
|
||||
|
||||
void*data = ar_alloc(length);
|
||||
void *data = ar_alloc_atomic(length);
|
||||
memcpy(data, arena_get(&translated->constants, offset), length);
|
||||
ArgonObject *object = ARGON_NULL;
|
||||
switch (type) {
|
||||
@@ -44,45 +63,34 @@ void load_const(Translated *translated, RuntimeState *state) {
|
||||
state->registers[to_register] = object;
|
||||
}
|
||||
|
||||
void run_instruction(Translated *translated, RuntimeState *state) {
|
||||
OperationType opcode = pop_bytecode(translated, state);
|
||||
void run_instruction(Translated *translated, RuntimeState *state,
|
||||
struct Stack stack) {
|
||||
OperationType opcode = pop_byte(translated, state);
|
||||
switch (opcode) {
|
||||
case OP_LOAD_NULL:
|
||||
state->registers[pop_bytecode(translated, state)] = ARGON_NULL;
|
||||
state->registers[pop_byte(translated, state)] = ARGON_NULL;
|
||||
break;
|
||||
case OP_LOAD_CONST:
|
||||
load_const(translated, state);
|
||||
break;
|
||||
case OP_LOAD_FUNCTION:
|
||||
load_argon_function(translated, state, stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void runtime(Translated translated) {
|
||||
RuntimeState init_runtime_state(Translated translated) {
|
||||
RuntimeState state = {
|
||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0};
|
||||
return state;
|
||||
}
|
||||
|
||||
while (state.head < translated.bytecode.size)
|
||||
run_instruction(&translated, &state);
|
||||
ArgonObject *runtime(Translated translated, RuntimeState state) {
|
||||
struct Stack stack = {NULL,NULL};
|
||||
state.head = 0;
|
||||
while (state.head < translated.bytecode.size) {
|
||||
run_instruction(&translated, &state, stack);
|
||||
}
|
||||
free(state.registers);
|
||||
}
|
||||
|
||||
static uint8_t siphash_key[16];
|
||||
|
||||
void generate_siphash_key() {
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd < 0 || read(fd, siphash_key, 16) != 16) {
|
||||
// Fallback or abort
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
uint64_t siphash64_bytes(const void *data, size_t len) {
|
||||
uint8_t out[8];
|
||||
if (siphash(data, len, siphash_key, out, sizeof(out)) != 0)
|
||||
return 0;
|
||||
|
||||
uint64_t hash = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
hash |= ((uint64_t)out[i]) << (8 * i);
|
||||
|
||||
return hash;
|
||||
return stack.scope;
|
||||
}
|
||||
@@ -8,14 +8,19 @@ typedef struct {
|
||||
size_t head;
|
||||
} RuntimeState;
|
||||
|
||||
typedef struct Stack {
|
||||
ArgonObject *scope;
|
||||
struct Stack *prev;
|
||||
} Stack;
|
||||
|
||||
void init_types();
|
||||
|
||||
void run_instruction(Translated *translated, RuntimeState *state);
|
||||
uint64_t pop_bytecode(Translated *translated, RuntimeState *state);
|
||||
|
||||
void runtime(Translated translated);
|
||||
void run_instruction(Translated *translated, RuntimeState *state, struct Stack stack);
|
||||
|
||||
uint64_t siphash64_bytes(const void *data, size_t len);
|
||||
RuntimeState init_runtime_state(Translated translated);
|
||||
|
||||
void generate_siphash_key();
|
||||
ArgonObject *runtime(Translated translated, RuntimeState state);
|
||||
|
||||
#endif // RUNTIME_H
|
||||
@@ -1,13 +1,15 @@
|
||||
# Bytecode Specification
|
||||
|
||||
all opcodes are uint8_t, and all operands are uint64_t unless marked with an asterisk (*), where it is marked as uint8_t
|
||||
|
||||
## OP_LOAD_CONST
|
||||
|
||||
loads and initialises a value from the constant buffer into the provided register.
|
||||
|
||||
this operation 4 operands.
|
||||
|
||||
1. the register to write to.
|
||||
2. the type of data from the constant buffer.
|
||||
1. the register to write to. (*)
|
||||
2. the type of data from the constant buffer. (*)
|
||||
3. the length of the data in the constant buffer.
|
||||
4. the offset in the constant buffer.
|
||||
|
||||
@@ -19,7 +21,7 @@ this operation takes 3 operands.
|
||||
|
||||
1. the length of the variable name.
|
||||
2. the offset in the constant buffer of the variable name.
|
||||
3. the register of the given value
|
||||
3. the register of the given value (*)
|
||||
|
||||
## OP_LOAD_NULL
|
||||
|
||||
@@ -27,4 +29,17 @@ sets a given register to null.
|
||||
|
||||
this operation takes 1 operand.
|
||||
|
||||
1. the register to set to null.
|
||||
1. the register to set to null. (*)
|
||||
|
||||
## OP_LOAD_FUNCTION
|
||||
|
||||
initilises a function to a given register.
|
||||
|
||||
1. the offset of the name of the function.
|
||||
2. the length of the name of the function.
|
||||
3. the number of arguments.
|
||||
4. the offset of the name of the argument.
|
||||
5. the length of the name of the argument.
|
||||
6. instruction 4 and 5 loop for each argument.
|
||||
7. the offset of the bytecode of the function.
|
||||
8. the length of the bytecode of the function.
|
||||
|
||||
@@ -9,7 +9,6 @@ size_t translate_parsed_declaration(Translated *translated,
|
||||
set_registers(translated, 1);
|
||||
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)
|
||||
@@ -18,14 +17,14 @@ size_t translate_parsed_declaration(Translated *translated,
|
||||
size_t offset =
|
||||
arena_push(&translated->constants, singleDeclaration->name, length);
|
||||
|
||||
push_instruction_code(translated, OP_DECLARE);
|
||||
push_instruction_byte(translated, OP_DECLARE);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_byte(translated, 0);
|
||||
}
|
||||
if (delcarations.size != 1) {
|
||||
push_instruction_code(translated, OP_LOAD_NULL);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_byte(translated, OP_LOAD_NULL);
|
||||
push_instruction_byte(translated, 0);
|
||||
}
|
||||
return first;
|
||||
}
|
||||
@@ -1,9 +1,37 @@
|
||||
#include "function.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue) {
|
||||
return 0;
|
||||
ParsedFunction *parsedFunction) {
|
||||
DArray main_bytecode = translated->bytecode;
|
||||
DArray _temp_bytecode;
|
||||
darray_init(&_temp_bytecode, sizeof(uint8_t));
|
||||
translated->bytecode = _temp_bytecode;
|
||||
translate_parsed(translated, parsedFunction->body);
|
||||
size_t function_bytecode_offset =
|
||||
arena_push(&translated->constants, translated->bytecode.data,
|
||||
translated->bytecode.size*translated->bytecode.element_size);
|
||||
size_t function_bytecode_length = translated->bytecode.size;
|
||||
darray_free(&translated->bytecode, NULL);
|
||||
translated->bytecode = main_bytecode;
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_FUNCTION);
|
||||
size_t offset = arena_push(&translated->constants, parsedFunction->name,
|
||||
strlen(parsedFunction->name));
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, strlen(parsedFunction->name));
|
||||
push_instruction_code(translated, parsedFunction->parameters.size);
|
||||
for (size_t i = 0; i < parsedFunction->parameters.size; i++) {
|
||||
char **parameter_name = darray_get(&parsedFunction->parameters, i);
|
||||
offset = arena_push(&translated->constants, *parameter_name,
|
||||
strlen(*parameter_name));
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, strlen(*parameter_name));
|
||||
}
|
||||
push_instruction_code(translated, function_bytecode_offset);
|
||||
push_instruction_code(translated, function_bytecode_length*translated->bytecode.element_size);
|
||||
return start;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
#ifndef BYTECODE_FUNCTION_H
|
||||
#define BYTECODE_FUNCTION_H
|
||||
#include "../translator.h"
|
||||
#include "../../parser/function/function.h"
|
||||
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue);
|
||||
ParsedFunction *parsedFunction);
|
||||
|
||||
#endif
|
||||
15
src/translator/identifier/identifier.c
Normal file
15
src/translator/identifier/identifier.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "../translator.h"
|
||||
#include "identifier.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t translate_parsed_identifier(Translated *translated, char* identifier) {
|
||||
size_t length = strlen(identifier);
|
||||
size_t identifier_pos = arena_push(&translated->constants, identifier, length);
|
||||
set_registers(translated, 1);
|
||||
size_t start = push_instruction_byte(translated, OP_IDENTIFIER);
|
||||
push_instruction_code(translated,length);
|
||||
push_instruction_code(translated, identifier_pos);
|
||||
return start;
|
||||
}
|
||||
8
src/translator/identifier/identifier.h
Normal file
8
src/translator/identifier/identifier.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef BYTECODE_IDENTIFIER_H
|
||||
#define BYTECODE_IDENTIFIER_H
|
||||
#include "../translator.h"
|
||||
#include "../../parser/assignable/identifier/identifier.h"
|
||||
|
||||
size_t translate_parsed_identifier(Translated *translated, char* identifier);
|
||||
|
||||
#endif
|
||||
@@ -10,10 +10,10 @@ size_t translate_parsed_number(Translated *translated, char *number_str, size_t
|
||||
size_t number_pos = arena_push(&translated->constants, number_str, length);
|
||||
set_registers(translated, to_register+1);
|
||||
|
||||
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
|
||||
push_instruction_code(translated, to_register);
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_CONST);
|
||||
push_instruction_byte(translated, to_register);
|
||||
|
||||
push_instruction_code(translated, TYPE_OP_NUMBER);
|
||||
push_instruction_byte(translated, TYPE_OP_NUMBER);
|
||||
push_instruction_code(translated,length);
|
||||
push_instruction_code(translated, number_pos);
|
||||
return start;
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
size_t translate_parsed_string(Translated *translated, ParsedString parsedString) {
|
||||
size_t string_pos = arena_push(&translated->constants, parsedString.string, parsedString.length);
|
||||
set_registers(translated, 1);
|
||||
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_code(translated, TYPE_OP_STRING);
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_CONST);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_byte(translated, TYPE_OP_STRING);
|
||||
push_instruction_code(translated,parsedString.length);
|
||||
push_instruction_code(translated, string_pos);
|
||||
return start;
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "translator.h"
|
||||
#include "../hash_data/hash_data.h"
|
||||
#include "../hashmap/hashmap.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "function/function.h"
|
||||
#include "identifier/identifier.h"
|
||||
#include "number/number.h"
|
||||
#include "string/string.h"
|
||||
#include <stddef.h>
|
||||
@@ -8,10 +12,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void uint64_to_bytes(uint64_t value, uint8_t bytes[8]) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
bytes[i] = (value >> (i * 8)) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
void arena_init(ConstantArena *arena) {
|
||||
arena->data = checked_malloc(CHUNK_SIZE);
|
||||
arena->capacity = CHUNK_SIZE;
|
||||
arena->size = 0;
|
||||
arena->hashmap = createHashmap();
|
||||
}
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size) {
|
||||
@@ -31,6 +42,7 @@ void arena_free(ConstantArena *arena) {
|
||||
free(arena->data);
|
||||
arena->capacity = 0;
|
||||
arena->size = 0;
|
||||
hashmap_free(arena->hashmap, NULL);
|
||||
}
|
||||
|
||||
void *arena_get(ConstantArena *arena, size_t offset) {
|
||||
@@ -38,42 +50,57 @@ void *arena_get(ConstantArena *arena, size_t offset) {
|
||||
}
|
||||
|
||||
size_t arena_push(ConstantArena *arena, const void *data, size_t length) {
|
||||
if (arena->size >= length) {
|
||||
for (size_t i = 0; i <= (arena->size - length); i++) {
|
||||
if (memcmp(data, arena->data + i, length) == 0) {
|
||||
return i;
|
||||
}
|
||||
uint64_t hash = siphash64_bytes(data, length, siphash_key);
|
||||
|
||||
// Look up offset in hashmap
|
||||
void *val = hashmap_lookup(arena->hashmap, hash);
|
||||
if (val != NULL) {
|
||||
size_t offset =
|
||||
(size_t)(uintptr_t)val - 1; // stored as pointer but really offset
|
||||
// Verify to avoid collision false positive
|
||||
if (memcmp(arena->data + offset, data, length) == 0) {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
// Not found: append data
|
||||
arena_resize(arena, arena->size + length);
|
||||
size_t offset = arena->size;
|
||||
memcpy(arena->data + arena->size, data, length);
|
||||
arena->size += length;
|
||||
|
||||
// Insert into hashmap: store offset as pointer-sized integer
|
||||
hashmap_insert(arena->hashmap, hash, (void *)data,
|
||||
(void *)(uintptr_t)offset + 1, 0);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
Translated init_translator() {
|
||||
Translated translated;
|
||||
translated.registerCount = 0;
|
||||
darray_init(&translated.bytecode, sizeof(uint64_t));
|
||||
darray_init(&translated.bytecode, sizeof(uint8_t));
|
||||
arena_init(&translated.constants);
|
||||
return translated;
|
||||
}
|
||||
|
||||
void set_instruction_code(Translated *translator, size_t offset,
|
||||
uint64_t code) {
|
||||
size_t *ptr = (translator->bytecode.data + offset);
|
||||
*ptr = code;
|
||||
}
|
||||
|
||||
size_t push_instruction_code(Translated *translator, uint64_t code) {
|
||||
code = htole64(code);
|
||||
size_t push_instruction_byte(Translated *translator, uint8_t byte) {
|
||||
size_t offset = translator->bytecode.size;
|
||||
darray_push(&translator->bytecode, &code);
|
||||
darray_push(&translator->bytecode, &byte);
|
||||
return offset;
|
||||
}
|
||||
|
||||
void set_registers(Translated *translator, size_t count) {
|
||||
size_t push_instruction_code(Translated *translator, uint64_t code) {
|
||||
size_t offset = translator->bytecode.size;
|
||||
uint8_t bytes[8];
|
||||
uint64_to_bytes(code, bytes);
|
||||
for (size_t i = 0; i < sizeof(bytes); i++) {
|
||||
darray_push(&translator->bytecode, &(bytes[i]));
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
void set_registers(Translated *translator, uint8_t count) {
|
||||
if (count > translator->registerCount)
|
||||
translator->registerCount = count;
|
||||
}
|
||||
@@ -81,16 +108,23 @@ void set_registers(Translated *translator, size_t count) {
|
||||
size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) {
|
||||
switch (parsedValue->type) {
|
||||
case AST_STRING:
|
||||
return translate_parsed_string(translated, *((ParsedString*)parsedValue->data));
|
||||
return translate_parsed_string(translated,
|
||||
*((ParsedString *)parsedValue->data));
|
||||
case AST_DECLARATION:
|
||||
return translate_parsed_declaration(translated, *((DArray*)parsedValue->data));
|
||||
return translate_parsed_declaration(translated,
|
||||
*((DArray *)parsedValue->data));
|
||||
case AST_NUMBER:
|
||||
return translate_parsed_number(translated, (char *)parsedValue->data, 0);
|
||||
case AST_NULL:
|
||||
set_registers(translated, 1);
|
||||
size_t output = push_instruction_code(translated, OP_LOAD_NULL);
|
||||
push_instruction_code(translated, 0);
|
||||
size_t output = push_instruction_byte(translated, OP_LOAD_NULL);
|
||||
push_instruction_byte(translated, 0);
|
||||
return output;
|
||||
case AST_FUNCTION:
|
||||
return translate_parsed_function(translated,
|
||||
(ParsedFunction *)parsedValue->data);
|
||||
case AST_IDENTIFIER:
|
||||
return translate_parsed_identifier(translated, (char *)parsedValue->data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,33 +4,37 @@
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "../memory.h"
|
||||
#include "../parser/parser.h"
|
||||
#include "../hashmap/hashmap.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL } OperationType;
|
||||
typedef enum { TYPE_OP_STRING = 255, TYPE_OP_NUMBER } types;
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER } OperationType;
|
||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
struct hashmap * hashmap;
|
||||
} ConstantArena;
|
||||
|
||||
typedef struct {
|
||||
size_t registerCount;
|
||||
uint8_t registerCount;
|
||||
DArray bytecode;
|
||||
ConstantArena constants;
|
||||
} Translated;
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size);
|
||||
|
||||
void *arena_get(ConstantArena *arena, size_t offset);
|
||||
|
||||
size_t arena_push(ConstantArena *arena, const void *data, size_t length);
|
||||
|
||||
void set_instruction_code(Translated * translator, size_t offset, uint64_t code);
|
||||
size_t push_instruction_byte(Translated *translator, uint8_t byte);
|
||||
|
||||
size_t push_instruction_code(Translated *translator, uint64_t code);
|
||||
|
||||
void set_registers(Translated *translator, size_t count);
|
||||
void set_registers(Translated *translator, uint8_t count);
|
||||
|
||||
Translated init_translator();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user