Compare commits
46 Commits
prerelease
...
release-v4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c856e7d654 | ||
|
|
a96023ced1 | ||
|
|
1908d9bbbb | ||
|
|
47db2ca27d | ||
|
|
2e7b3b4baa | ||
|
|
24163e3389 | ||
|
|
0c0832d131 | ||
|
|
1742a0c52d | ||
|
|
224039ba43 | ||
|
|
5e7ce495e4 | ||
|
|
35a0f35cf8 | ||
|
|
f9490ceac0 | ||
|
|
4cda311008 | ||
|
|
b3aa653076 | ||
|
|
fc3321bcf0 | ||
|
|
868b3bfc3d | ||
|
|
f5e241aba0 | ||
|
|
8c3ee3fe05 | ||
|
|
eb285b6e8f | ||
|
|
78a1edd572 | ||
|
|
b905026010 | ||
|
|
677afd9433 | ||
|
|
757da3f973 | ||
|
|
5a86510c3b | ||
|
|
fb8b6a89ae | ||
|
|
6ddf9953e7 | ||
|
|
1609227a42 | ||
|
|
51f6a88ce8 | ||
|
|
f420273471 | ||
|
|
4b2a747338 | ||
|
|
5277814af0 | ||
|
|
8928ab2d99 | ||
|
|
d08b307c6e | ||
|
|
6474329afc | ||
|
|
c49e67c839 | ||
|
|
25cb96e473 | ||
|
|
f11890a8b3 | ||
|
|
59b1d222c2 | ||
|
|
2fba132016 | ||
|
|
bddc2cdc79 | ||
|
|
e1b80b42d9 | ||
|
|
dab86925b4 | ||
|
|
0f45052dce | ||
|
|
68f4207216 | ||
|
|
f9f8ca08c6 | ||
|
|
0666b02c13 |
118
.github/workflows/release.yml
vendored
118
.github/workflows/release.yml
vendored
@@ -10,8 +10,8 @@ on:
|
|||||||
- '*' # Any tag
|
- '*' # Any tag
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-linux:
|
build-linux-x86_64:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest # build both architectures
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
@@ -51,18 +51,17 @@ jobs:
|
|||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: linux-artifact
|
name: linux-x86_64-artifact
|
||||||
path: ${{ env.TAR_NAME }}
|
path: ${{ env.TAR_NAME }}
|
||||||
|
build-linux-arm64:
|
||||||
build-macos:
|
runs-on: ubuntu-22.04-arm # build both architectures
|
||||||
runs-on: macos-latest
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- name: Install build tools
|
- name: Install build tools
|
||||||
run: brew install flex bison
|
run: sudo apt-get update && sudo apt-get install -y flex bison
|
||||||
|
|
||||||
- name: Setup Python
|
- name: Setup Python
|
||||||
uses: actions/setup-python@v4
|
uses: actions/setup-python@v4
|
||||||
@@ -95,7 +94,74 @@ jobs:
|
|||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: macos-artifact
|
name: linux-arm64-artifact
|
||||||
|
path: ${{ env.TAR_NAME }}
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
runs-on: macos-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
arch: [x86_64, arm64] # build both architectures
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Install build tools
|
||||||
|
run: brew install flex bison
|
||||||
|
|
||||||
|
- name: Setup Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: '3.x'
|
||||||
|
|
||||||
|
- name: Install Conan
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install conan
|
||||||
|
if [ "${{ matrix.arch }}" = "x86_64" ] && [ "$(uname -m)" = "arm64" ]; then
|
||||||
|
arch -x86_64 conan profile detect
|
||||||
|
else
|
||||||
|
conan profile detect
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: |
|
||||||
|
BUILD_DIR="build_${{ matrix.arch }}"
|
||||||
|
mkdir -p "$BUILD_DIR"
|
||||||
|
cd "$BUILD_DIR"
|
||||||
|
|
||||||
|
# Use Rosetta for x86_64 builds on Apple Silicon
|
||||||
|
if [ "${{ matrix.arch }}" = "x86_64" ] && [ "$(uname -m)" = "arm64" ]; then
|
||||||
|
arch -x86_64 bash -c "
|
||||||
|
export CMAKE_OSX_ARCHITECTURES=x86_64
|
||||||
|
conan install .. --build=missing
|
||||||
|
conan build ..
|
||||||
|
"
|
||||||
|
else
|
||||||
|
export CMAKE_OSX_ARCHITECTURES="${{ matrix.arch }}"
|
||||||
|
conan install .. --build=missing
|
||||||
|
conan build ..
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd ..
|
||||||
|
|
||||||
|
- name: Package
|
||||||
|
run: |
|
||||||
|
TAG=${GITHUB_REF##refs/tags/}
|
||||||
|
OS="macos"
|
||||||
|
ARCH="${{ matrix.arch }}"
|
||||||
|
FOLDER="chloride-$TAG-$OS-$ARCH"
|
||||||
|
TAR="$FOLDER.tar.gz"
|
||||||
|
mv build/bin "$FOLDER"
|
||||||
|
cp LICENSE.txt "$FOLDER"
|
||||||
|
tar -czf "$TAR" "$FOLDER"
|
||||||
|
echo "TAR_NAME=$TAR" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: macos-${{ matrix.arch }}-artifact
|
||||||
path: ${{ env.TAR_NAME }}
|
path: ${{ env.TAR_NAME }}
|
||||||
|
|
||||||
build-windows:
|
build-windows:
|
||||||
@@ -117,24 +183,33 @@ jobs:
|
|||||||
base-devel
|
base-devel
|
||||||
mingw-w64-x86_64-gcc
|
mingw-w64-x86_64-gcc
|
||||||
mingw-w64-x86_64-make
|
mingw-w64-x86_64-make
|
||||||
mingw-w64-x86_64-gmp
|
mingw-w64-x86_64-cmake
|
||||||
mingw-w64-x86_64-gc
|
mingw-w64-x86_64-python
|
||||||
|
mingw-w64-x86_64-python-pip
|
||||||
|
msys/flex
|
||||||
|
|
||||||
|
- name: Install Conan
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install conan
|
||||||
|
conan profile detect
|
||||||
|
|
||||||
- name: Build Project
|
- name: Build Project
|
||||||
run: |
|
run: |
|
||||||
make windows
|
conan install . --profile windows-profile.txt --build=missing
|
||||||
|
conan build . --profile windows-profile.txt
|
||||||
|
|
||||||
- name: Package
|
- name: Package
|
||||||
run: |
|
run: |
|
||||||
$TAG = $env:GITHUB_REF -replace 'refs/tags/', ''
|
TAG=${GITHUB_REF##refs/tags/}
|
||||||
$ARCH = if ([Environment]::Is64BitOperatingSystem) { 'x64' } else { 'x86' }
|
OS="windows"
|
||||||
$FOLDER = "chloride-$TAG-windows-$ARCH"
|
ARCH=$(uname -m)
|
||||||
$ZIP = "$FOLDER.zip"
|
FOLDER="chloride-$TAG-$OS-$ARCH"
|
||||||
Rename-Item bin $FOLDER
|
TAR="$FOLDER.tar.gz"
|
||||||
Copy-Item LICENSE.txt $FOLDER
|
mv build/bin "$FOLDER"
|
||||||
Compress-Archive -Path $FOLDER -DestinationPath $ZIP
|
cp LICENSE.txt "$FOLDER"
|
||||||
echo "TAR_NAME=$ZIP" >> $env:GITHUB_ENV
|
tar -czf "$TAR" "$FOLDER"
|
||||||
shell: pwsh
|
echo "TAR_NAME=$TAR" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
@@ -144,12 +219,13 @@ jobs:
|
|||||||
|
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [build-linux, build-macos, build-windows]
|
needs: [build-linux-x86_64, build-linux-arm64, build-macos, build-windows]
|
||||||
steps:
|
steps:
|
||||||
- name: Download artifacts
|
- name: Download artifacts
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
path: ./artifacts
|
path: ./artifacts
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
- name: Create GitHub Release
|
- name: Create GitHub Release
|
||||||
uses: ncipollo/release-action@v1
|
uses: ncipollo/release-action@v1
|
||||||
|
|||||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -11,3 +11,6 @@
|
|||||||
[submodule "external/libdye"]
|
[submodule "external/libdye"]
|
||||||
path = external/libdye
|
path = external/libdye
|
||||||
url = https://github.com/Ugric/libdye.git
|
url = https://github.com/Ugric/libdye.git
|
||||||
|
[submodule "external/linenoise"]
|
||||||
|
path = external/linenoise
|
||||||
|
url = https://github.com/antirez/linenoise
|
||||||
|
|||||||
@@ -33,8 +33,16 @@ add_custom_command(
|
|||||||
# Step 2: Custom target for lexer
|
# Step 2: Custom target for lexer
|
||||||
add_custom_target(GenerateLexer DEPENDS ${LEXER_C} ${LEXER_H})
|
add_custom_target(GenerateLexer DEPENDS ${LEXER_C} ${LEXER_H})
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c ${CFILES} ${LEXER_C}
|
||||||
|
)
|
||||||
|
|
||||||
|
if(NOT WIN32)
|
||||||
|
list(APPEND SOURCES external/linenoise/linenoise.c)
|
||||||
|
endif()
|
||||||
|
|
||||||
# Step 3: Add executable
|
# Step 3: Add executable
|
||||||
add_executable(argon external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c ${CFILES} ${LEXER_C})
|
add_executable(argon ${SOURCES})
|
||||||
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
|
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
|
||||||
target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/cwalk/include)
|
target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/cwalk/include)
|
||||||
target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/libdye/include)
|
target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/libdye/include)
|
||||||
@@ -52,14 +60,12 @@ find_package(BDWgc REQUIRED)
|
|||||||
find_package(gmp REQUIRED)
|
find_package(gmp REQUIRED)
|
||||||
|
|
||||||
target_compile_options(argon PRIVATE -O3 -Wall -Wextra -Wno-unused-function -s)
|
target_compile_options(argon PRIVATE -O3 -Wall -Wextra -Wno-unused-function -s)
|
||||||
if(NOT APPLE)
|
|
||||||
target_link_options(argon PRIVATE -static)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(argon PRIVATE
|
target_link_libraries(argon PRIVATE
|
||||||
BDWgc::BDWgc
|
BDWgc::BDWgc
|
||||||
gmp::gmp
|
gmp::gmp
|
||||||
m
|
m
|
||||||
|
$<$<PLATFORM_ID:Windows>:bcrypt>
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(argon PRIVATE
|
target_include_directories(argon PRIVATE
|
||||||
|
|||||||
26
Makefile
26
Makefile
@@ -6,20 +6,13 @@
|
|||||||
BINARY = bin/argon
|
BINARY = bin/argon
|
||||||
FLEX_TOOL = flex
|
FLEX_TOOL = flex
|
||||||
|
|
||||||
CFILES = external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c $(shell find src -name '*.c')
|
CFILES = external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c external/linenoise/linenoise.c $(shell find src -name '*.c')
|
||||||
|
|
||||||
# If target is "windows", override FLEX_TOOL
|
|
||||||
ifeq ($(MAKECMDGOALS),windows)
|
|
||||||
BINARY = bin/argon.exe
|
|
||||||
FLEX_TOOL = win_flex
|
|
||||||
|
|
||||||
CFILES = external/xxhash/xxhash.c external/cwalk/src/cwalk.c external/libdye/src/dye.c $(shell dir /b /s src\*.c)
|
|
||||||
endif
|
|
||||||
|
|
||||||
LEXER_SRC = src/lexer/lex.l
|
LEXER_SRC = src/lexer/lex.l
|
||||||
LEXER_C = src/lexer/lex.yy.c
|
LEXER_C = src/lexer/lex.yy.c
|
||||||
LEXER_H = src/lexer/lex.yy.h
|
LEXER_H = src/lexer/lex.yy.h
|
||||||
CFLAGS = $(ARCHFLAGS) -lm -lgc -lgmp -Wall -Wextra -Wno-unused-function -Werror=unused-result -Iexternal/cwalk/include -Iexternal/libdye/include
|
CFLAGS = $(ARCHFLAGS) -lm -lgc -lgmp -Wall -Wextra -Wno-unused-function -Werror=unused-result -Iexternal/cwalk/include -Iexternal/libdye/include
|
||||||
|
LDFLAGS = -lgc -lgmp -lm
|
||||||
|
|
||||||
all: $(BINARY)
|
all: $(BINARY)
|
||||||
|
|
||||||
@@ -29,16 +22,11 @@ $(LEXER_C) $(LEXER_H): $(LEXER_SRC)
|
|||||||
|
|
||||||
$(BINARY): $(CFILES) $(LEXER_C) $(LEXER_H)
|
$(BINARY): $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
gcc -O3 -o $(BINARY) $(CFILES) $(CFLAGS) -s
|
gcc -O3 -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS} -s
|
||||||
|
|
||||||
windows: $(CFILES) $(LEXER_C) $(LEXER_H)
|
|
||||||
find external/xxhash external/cwalk external/libdye src -name '*.c' > sources.txt
|
|
||||||
mkdir -p bin
|
|
||||||
gcc -O3 -march=native -o $(BINARY) @sources.txt $(CFLAGS)
|
|
||||||
|
|
||||||
native: $(CFILES) $(LEXER_C) $(LEXER_H)
|
native: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
gcc -O3 -march=native -o $(BINARY) $(CFILES) $(CFLAGS)
|
gcc -O3 -march=native -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
|
||||||
|
|
||||||
debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
@@ -46,13 +34,13 @@ debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
|||||||
|
|
||||||
full-debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
full-debug: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
gcc -g -O0 -fsanitize=address -fno-omit-frame-pointer -o $(BINARY) $(CFILES) $(CFLAGS)
|
gcc -g -O0 -fsanitize=address -fno-omit-frame-pointer -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
|
||||||
|
|
||||||
optimised: $(CFILES) $(LEXER_C) $(LEXER_H)
|
optimised: $(CFILES) $(LEXER_C) $(LEXER_H)
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
gcc -O3 -fprofile-generate -o $(BINARY) $(CFILES) $(CFLAGS)
|
gcc -O3 -fprofile-generate -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
|
||||||
${BINARY} rand_test.ar
|
${BINARY} rand_test.ar
|
||||||
gcc -O3 -fprofile-use -o $(BINARY) $(CFILES) $(CFLAGS)
|
gcc -O3 -fprofile-use -o $(BINARY) $(CFILES) $(CFLAGS) ${LDFLAGS}
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|||||||
@@ -33,11 +33,6 @@ class ArgonConan(ConanFile):
|
|||||||
os.environ["CONAN_NON_INTERACTIVE"] = "1"
|
os.environ["CONAN_NON_INTERACTIVE"] = "1"
|
||||||
tc = CMakeToolchain(self)
|
tc = CMakeToolchain(self)
|
||||||
|
|
||||||
if os.name == "nt": # Windows
|
|
||||||
flex_path = which("win_flex") or which("win_flex.exe")
|
|
||||||
if not flex_path:
|
|
||||||
raise Exception("win_flex not found in PATH. Install winflexbison via choco.")
|
|
||||||
else:
|
|
||||||
flex_path = which("flex")
|
flex_path = which("flex")
|
||||||
if not flex_path:
|
if not flex_path:
|
||||||
raise Exception("Flex not found in system PATH. Please install flex on Linux/macOS.")
|
raise Exception("Flex not found in system PATH. Please install flex on Linux/macOS.")
|
||||||
|
|||||||
1
external/linenoise
vendored
Submodule
1
external/linenoise
vendored
Submodule
Submodule external/linenoise added at d895173d67
91
src/main.c
91
src/main.c
@@ -11,6 +11,8 @@
|
|||||||
#include "lexer/token.h"
|
#include "lexer/token.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
|
#include "returnTypes.h"
|
||||||
|
#include "shell.h"
|
||||||
#include "runtime/runtime.h"
|
#include "runtime/runtime.h"
|
||||||
#include "translator/translator.h"
|
#include "translator/translator.h"
|
||||||
|
|
||||||
@@ -66,8 +68,9 @@ static inline uint64_t htole64(uint64_t x) { return x; }
|
|||||||
#define htole64(x) OSSwapHostToLittleInt64(x)
|
#define htole64(x) OSSwapHostToLittleInt64(x)
|
||||||
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||||
// Add others as needed
|
// Add others as needed
|
||||||
#else
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
||||||
#error "Unsupported platform"
|
#include <stdlib.h>
|
||||||
|
#include <sys/endian.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *get_current_directory() {
|
char *get_current_directory() {
|
||||||
@@ -277,18 +280,20 @@ FAILED:
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Execution execute(char *path, Stack *stack) {
|
Translated load_argon_file(char *path, ArErr *err) {
|
||||||
clock_t start, end;
|
clock_t start, end;
|
||||||
|
clock_t beginning = clock();
|
||||||
double time_spent, total_time_spent = 0;
|
double time_spent, total_time_spent = 0;
|
||||||
|
|
||||||
const char *basename_ptr;
|
const char *basename_ptr;
|
||||||
size_t basename_length;
|
size_t basename_length;
|
||||||
cwk_path_get_basename(path, &basename_ptr, &basename_length);
|
cwk_path_get_basename(path, &basename_ptr, &basename_length);
|
||||||
|
|
||||||
if (!basename_ptr)
|
if (!basename_ptr) {
|
||||||
return (Execution){create_err(0, 0, 0, NULL, "Path Error",
|
*err = create_err(0, 0, 0, NULL, "Path Error", "path has no basename '%s'",
|
||||||
"path has no basename '%s'", path),
|
path);
|
||||||
(Stack){NULL, NULL}};
|
return (Translated){};
|
||||||
|
}
|
||||||
|
|
||||||
char basename[FILENAME_MAX];
|
char basename[FILENAME_MAX];
|
||||||
memcpy(basename, basename_ptr, basename_length);
|
memcpy(basename, basename_ptr, basename_length);
|
||||||
@@ -312,9 +317,9 @@ Execution execute(char *path, Stack *stack) {
|
|||||||
|
|
||||||
FILE *file = fopen(path, "r");
|
FILE *file = fopen(path, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return (Execution){create_err(0, 0, 0, NULL, "File Error",
|
*err = create_err(0, 0, 0, NULL, "File Error", "Unable to open file '%s'",
|
||||||
"Unable to open file '%s'", path),
|
path);
|
||||||
(Stack){NULL, NULL}};
|
return (Translated){};
|
||||||
}
|
}
|
||||||
|
|
||||||
XXH3_state_t *hash_state = XXH3_createState();
|
XXH3_state_t *hash_state = XXH3_createState();
|
||||||
@@ -338,14 +343,13 @@ Execution execute(char *path, Stack *stack) {
|
|||||||
|
|
||||||
LexerState state = {path, file, 0, 0, &tokens};
|
LexerState state = {path, file, 0, 0, &tokens};
|
||||||
start = clock();
|
start = clock();
|
||||||
ArErr err = lexer(state);
|
*err = lexer(state);
|
||||||
if (err.exists) {
|
if (err->exists) {
|
||||||
darray_free(&tokens, free_token);
|
darray_free(&tokens, free_token);
|
||||||
return (Execution){err, (Stack){NULL, NULL}};
|
return (Translated){};
|
||||||
}
|
}
|
||||||
end = clock();
|
end = clock();
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
total_time_spent += time_spent;
|
|
||||||
fprintf(stderr, "Lexer time taken: %f seconds\n", time_spent);
|
fprintf(stderr, "Lexer time taken: %f seconds\n", time_spent);
|
||||||
fclose(state.file);
|
fclose(state.file);
|
||||||
|
|
||||||
@@ -354,35 +358,30 @@ Execution execute(char *path, Stack *stack) {
|
|||||||
darray_init(&ast, sizeof(ParsedValue));
|
darray_init(&ast, sizeof(ParsedValue));
|
||||||
|
|
||||||
start = clock();
|
start = clock();
|
||||||
err = parser(path, &ast, &tokens, false);
|
*err = parser(path, &ast, &tokens, false);
|
||||||
if (err.exists) {
|
|
||||||
darray_free(&tokens, free_token);
|
darray_free(&tokens, free_token);
|
||||||
|
if (err->exists) {
|
||||||
darray_free(&ast, free_parsed);
|
darray_free(&ast, free_parsed);
|
||||||
return (Execution){err, (Stack){NULL, NULL}};
|
return (Translated){};
|
||||||
}
|
}
|
||||||
end = clock();
|
end = clock();
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
total_time_spent += time_spent;
|
|
||||||
fprintf(stderr, "Parser time taken: %f seconds\n", time_spent);
|
fprintf(stderr, "Parser time taken: %f seconds\n", time_spent);
|
||||||
darray_free(&tokens, free_token);
|
|
||||||
|
|
||||||
start = clock();
|
start = clock();
|
||||||
|
|
||||||
translated = init_translator(path);
|
translated = init_translator(path);
|
||||||
err = translate(&translated, &ast);
|
*err = translate(&translated, &ast);
|
||||||
if (err.exists) {
|
darray_free(&ast, free_parsed);
|
||||||
|
if (err->exists) {
|
||||||
darray_free(&translated.bytecode, NULL);
|
darray_free(&translated.bytecode, NULL);
|
||||||
free(translated.constants.data);
|
free(translated.constants.data);
|
||||||
hashmap_free(translated.constants.hashmap, NULL);
|
hashmap_free(translated.constants.hashmap, NULL);
|
||||||
darray_free(&ast, free_parsed);
|
return (Translated){};
|
||||||
return (Execution){err, (Stack){NULL, NULL}};
|
|
||||||
}
|
}
|
||||||
end = clock();
|
end = clock();
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
total_time_spent += time_spent;
|
|
||||||
fprintf(stderr, "Translation time taken: %f seconds\n", time_spent);
|
fprintf(stderr, "Translation time taken: %f seconds\n", time_spent);
|
||||||
|
|
||||||
darray_free(&ast, free_parsed);
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
malloc_trim(0);
|
malloc_trim(0);
|
||||||
#endif
|
#endif
|
||||||
@@ -444,18 +443,10 @@ Execution execute(char *path, Stack *stack) {
|
|||||||
gc_translated.constants.capacity = translated.constants.capacity;
|
gc_translated.constants.capacity = translated.constants.capacity;
|
||||||
darray_free(&translated.bytecode, NULL);
|
darray_free(&translated.bytecode, NULL);
|
||||||
free(translated.constants.data);
|
free(translated.constants.data);
|
||||||
|
total_time_spent = (double)(clock() - beginning) / CLOCKS_PER_SEC;
|
||||||
start = clock();
|
fprintf(stderr, "total time taken loading file (%s): %f seconds\n", path,
|
||||||
RuntimeState state = init_runtime_state(gc_translated, path);
|
total_time_spent);
|
||||||
Stack *main_scope = create_scope(stack);
|
return gc_translated;
|
||||||
ArErr err = runtime(gc_translated, state, main_scope);
|
|
||||||
end = clock();
|
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
|
||||||
total_time_spent += time_spent;
|
|
||||||
fprintf(stderr,"Execution time taken: %f seconds\n", time_spent);
|
|
||||||
fprintf(stderr,"total time taken: %f seconds\n", total_time_spent);
|
|
||||||
|
|
||||||
return (Execution){err, *main_scope};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@@ -465,18 +456,32 @@ int main(int argc, char *argv[]) {
|
|||||||
generate_siphash_key(siphash_key);
|
generate_siphash_key(siphash_key);
|
||||||
bootstrap_types();
|
bootstrap_types();
|
||||||
bootstrap_globals();
|
bootstrap_globals();
|
||||||
char *CWD = get_current_directory();
|
|
||||||
if (argc <= 1)
|
if (argc <= 1)
|
||||||
return -1;
|
return shell();
|
||||||
|
char *CWD = get_current_directory();
|
||||||
char *path_non_absolute = argv[1];
|
char *path_non_absolute = argv[1];
|
||||||
char path[FILENAME_MAX];
|
char path[FILENAME_MAX];
|
||||||
cwk_path_get_absolute(CWD, path_non_absolute, path, sizeof(path));
|
cwk_path_get_absolute(CWD, path_non_absolute, path, sizeof(path));
|
||||||
free(CWD);
|
free(CWD);
|
||||||
Execution resp = execute(path, Global_Scope);
|
ArErr err;
|
||||||
|
Translated translated = load_argon_file(path, &err);
|
||||||
|
if (err.exists) {
|
||||||
|
output_err(err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
clock_t start = clock(), end;
|
||||||
|
RuntimeState state = init_runtime_state(translated, path);
|
||||||
|
Stack *main_scope = create_scope(Global_Scope);
|
||||||
|
err = runtime(translated, state, main_scope);
|
||||||
|
|
||||||
|
end = clock();
|
||||||
|
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
|
fprintf(stderr, "Execution time taken: %f seconds\n", time_spent);
|
||||||
|
|
||||||
if (runtime_hash_table)
|
if (runtime_hash_table)
|
||||||
hashmap_free(runtime_hash_table, NULL);
|
hashmap_free(runtime_hash_table, NULL);
|
||||||
if (resp.err.exists) {
|
if (err.exists) {
|
||||||
output_err(resp.err);
|
output_err(err);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Your main thread code
|
// Your main thread code
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#include "../../memory.h"
|
#include "../../memory.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
#include <gmp.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -180,7 +179,8 @@ ParsedValueReturn parse_number(Token *token, char *path) {
|
|||||||
mpq_init(*r_ptr);
|
mpq_init(*r_ptr);
|
||||||
int err = mpq_set_decimal_str_exp(*r_ptr, token->value, token->length);
|
int err = mpq_set_decimal_str_exp(*r_ptr, token->value, token->length);
|
||||||
if (err) {
|
if (err) {
|
||||||
free_parsed(parsedValue);
|
mpq_clear(*r_ptr);
|
||||||
|
free(r_ptr);
|
||||||
free(parsedValue);
|
free(parsedValue);
|
||||||
return (ParsedValueReturn){create_err(token->line, token->column,
|
return (ParsedValueReturn){create_err(token->line, token->column,
|
||||||
token->length, path, "Parsing Error",
|
token->length, path, "Parsing Error",
|
||||||
@@ -190,3 +190,4 @@ ParsedValueReturn parse_number(Token *token, char *path) {
|
|||||||
parsedValue->data = r_ptr;
|
parsedValue->data = r_ptr;
|
||||||
return (ParsedValueReturn){no_err, parsedValue};
|
return (ParsedValueReturn){no_err, parsedValue};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,4 @@ typedef struct ArErr {
|
|||||||
char type[32];
|
char type[32];
|
||||||
char message[ERR_MSG_MAX_LEN];
|
char message[ERR_MSG_MAX_LEN];
|
||||||
} ArErr;
|
} ArErr;
|
||||||
|
|
||||||
typedef struct Execution {
|
|
||||||
ArErr err;
|
|
||||||
Stack stack;
|
|
||||||
} Execution;
|
|
||||||
#endif // RETURN_TYPES_
|
#endif // RETURN_TYPES_
|
||||||
@@ -15,19 +15,19 @@ ArgonObject *ARGON_TYPE_TYPE___get_attr__(size_t argc, ArgonObject **argv,
|
|||||||
return ARGON_NULL;
|
return ARGON_NULL;
|
||||||
}
|
}
|
||||||
ArgonObject *to_access = argv[0];
|
ArgonObject *to_access = argv[0];
|
||||||
ArgonObject *access = argv[1];
|
bool check_field = argv[1] == ARGON_TRUE;
|
||||||
bool check_field = argv[2] == ARGON_TRUE;
|
|
||||||
if (check_field) {
|
if (check_field) {
|
||||||
|
ArgonObject *access = argv[2];
|
||||||
ArgonObject *value = get_field_l(to_access, access->value.as_str.data,
|
ArgonObject *value = get_field_l(to_access, access->value.as_str.data,
|
||||||
access->value.as_str.length, true, false);
|
access->value.as_str.length, true, false);
|
||||||
if (value)
|
if (value)
|
||||||
return value;
|
return value;
|
||||||
}
|
|
||||||
ArgonObject *name = get_field_for_class(
|
ArgonObject *name = get_field_for_class(
|
||||||
get_field(to_access, "__class__", false, false), "__name__", to_access);
|
get_field(to_access, "__class__", false, false), "__name__", to_access);
|
||||||
*err = create_err(0, 0, 0, "", "Runtime Error",
|
*err = create_err(
|
||||||
"'%.*s' object has no attribute '%.*s'",
|
0, 0, 0, "", "Runtime Error", "'%.*s' object has no attribute '%.*s'",
|
||||||
(int)name->value.as_str.length, name->value.as_str.data,
|
(int)name->value.as_str.length, name->value.as_str.data,
|
||||||
(int)access->value.as_str.length, access->value.as_str.data);
|
(int)access->value.as_str.length, access->value.as_str.data);
|
||||||
|
}
|
||||||
return ARGON_NULL;
|
return ARGON_NULL;
|
||||||
}
|
}
|
||||||
@@ -16,8 +16,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <psapi.h>
|
#ifndef _WIN32_WINNT
|
||||||
|
#define _WIN32_WINNT 0x0602
|
||||||
|
#endif
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <psapi.h>
|
||||||
|
|
||||||
double get_memory_usage_mb() {
|
double get_memory_usage_mb() {
|
||||||
PROCESS_MEMORY_COUNTERS pmc;
|
PROCESS_MEMORY_COUNTERS pmc;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "number.h"
|
#include "number.h"
|
||||||
#include "../functions/functions.h"
|
#include "../functions/functions.h"
|
||||||
#include "../string/string.h"
|
#include "../string/string.h"
|
||||||
|
#include <gmp-x86_64.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -126,6 +127,56 @@ ArgonObject *ARGON_NUMBER_TYPE___subtract__(size_t argc, ArgonObject **argv,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArgonObject *ARGON_NUMBER_TYPE___multiply__(size_t argc, ArgonObject **argv,
|
||||||
|
ArErr *err, RuntimeState *state) {
|
||||||
|
(void)state;
|
||||||
|
if (argc != 2) {
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"__multiply__ expects 2 arguments, got %" PRIu64, argc);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
mpq_t r;
|
||||||
|
mpq_init(r);
|
||||||
|
if (argv[1]->type != TYPE_NUMBER) {
|
||||||
|
ArgonObject *type_name = get_field_for_class(
|
||||||
|
get_field(argv[1], "__class__", false, false), "__name__", argv[1]);
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"__multiply__ cannot perform multiplication between number and %.*s",
|
||||||
|
type_name->value.as_str.length,
|
||||||
|
type_name->value.as_str.data);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
mpq_mul(r, *argv[0]->value.as_number, *argv[1]->value.as_number);
|
||||||
|
ArgonObject *result = new_number_object(r);
|
||||||
|
mpq_clear(r);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArgonObject *ARGON_NUMBER_TYPE___division__(size_t argc, ArgonObject **argv,
|
||||||
|
ArErr *err, RuntimeState *state) {
|
||||||
|
(void)state;
|
||||||
|
if (argc != 2) {
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"__division__ expects 2 arguments, got %" PRIu64, argc);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
mpq_t r;
|
||||||
|
mpq_init(r);
|
||||||
|
if (argv[1]->type != TYPE_NUMBER) {
|
||||||
|
ArgonObject *type_name = get_field_for_class(
|
||||||
|
get_field(argv[1], "__class__", false, false), "__name__", argv[1]);
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"__division__ cannot perform division between number and %.*s",
|
||||||
|
type_name->value.as_str.length,
|
||||||
|
type_name->value.as_str.data);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
mpq_div(r, *argv[0]->value.as_number, *argv[1]->value.as_number);
|
||||||
|
ArgonObject *result = new_number_object(r);
|
||||||
|
mpq_clear(r);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
ArgonObject *ARGON_NUMBER_TYPE___string__(size_t argc, ArgonObject **argv,
|
ArgonObject *ARGON_NUMBER_TYPE___string__(size_t argc, ArgonObject **argv,
|
||||||
ArErr *err, RuntimeState *state) {
|
ArErr *err, RuntimeState *state) {
|
||||||
(void)state;
|
(void)state;
|
||||||
@@ -298,6 +349,12 @@ void create_ARGON_NUMBER_TYPE() {
|
|||||||
add_field(ARGON_NUMBER_TYPE, "__subtract__",
|
add_field(ARGON_NUMBER_TYPE, "__subtract__",
|
||||||
create_argon_native_function("__subtract__",
|
create_argon_native_function("__subtract__",
|
||||||
ARGON_NUMBER_TYPE___subtract__));
|
ARGON_NUMBER_TYPE___subtract__));
|
||||||
|
add_field(ARGON_NUMBER_TYPE, "__multiply__",
|
||||||
|
create_argon_native_function("__multiply__",
|
||||||
|
ARGON_NUMBER_TYPE___multiply__));
|
||||||
|
add_field(ARGON_NUMBER_TYPE, "__division__",
|
||||||
|
create_argon_native_function("__division__",
|
||||||
|
ARGON_NUMBER_TYPE___division__));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mpz_init_gc_managed(mpz_t z, size_t limbs_count) {
|
void mpz_init_gc_managed(mpz_t z, size_t limbs_count) {
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ ArgonObject *ACCESS_FUNCTION;
|
|||||||
ArgonObject *ADDITION_FUNCTION;
|
ArgonObject *ADDITION_FUNCTION;
|
||||||
ArgonObject *SUBTRACTION_FUNCTION;
|
ArgonObject *SUBTRACTION_FUNCTION;
|
||||||
ArgonObject *MULTIPLY_FUNCTION;
|
ArgonObject *MULTIPLY_FUNCTION;
|
||||||
ArgonObject *DIVIDE_FUNCTION;
|
ArgonObject *DIVISION_FUNCTION;
|
||||||
ArgonObject *POWER_FUNCTION;
|
ArgonObject *POWER_FUNCTION;
|
||||||
ArgonObject *MODULO_FUNCTION;
|
ArgonObject *MODULO_FUNCTION;
|
||||||
ArgonObject *FLOORDIV_FUNCTION;
|
ArgonObject *FLOORDIVISION_FUNCTION;
|
||||||
|
|
||||||
ArgonObject *ARGON_ADDITION_FUNCTION(size_t argc, ArgonObject **argv,
|
ArgonObject *ARGON_ADDITION_FUNCTION(size_t argc, ArgonObject **argv,
|
||||||
ArErr *err, RuntimeState *state) {
|
ArErr *err, RuntimeState *state) {
|
||||||
@@ -70,7 +70,7 @@ ArgonObject *ARGON_SUBTRACTION_FUNCTION(size_t argc, ArgonObject **argv,
|
|||||||
ArErr *err, RuntimeState *state) {
|
ArErr *err, RuntimeState *state) {
|
||||||
if (argc < 1) {
|
if (argc < 1) {
|
||||||
*err = create_err(0, 0, 0, "", "Runtime Error",
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
"add expects at least 1 argument, got %" PRIu64, argc);
|
"subtract expects at least 1 argument, got %" PRIu64, argc);
|
||||||
return ARGON_NULL;
|
return ARGON_NULL;
|
||||||
}
|
}
|
||||||
ArgonObject *output = argv[0];
|
ArgonObject *output = argv[0];
|
||||||
@@ -91,6 +91,56 @@ ArgonObject *ARGON_SUBTRACTION_FUNCTION(size_t argc, ArgonObject **argv,
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArgonObject *ARGON_MULTIPLY_FUNCTION(size_t argc, ArgonObject **argv,
|
||||||
|
ArErr *err, RuntimeState *state) {
|
||||||
|
if (argc < 1) {
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"multiply expects at least 1 argument, got %" PRIu64, argc);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
ArgonObject *output = argv[0];
|
||||||
|
for (size_t i = 1; i < argc; i++) {
|
||||||
|
ArgonObject *__multiply__ = get_field_for_class(
|
||||||
|
get_field(output, "__class__", false, false), "__multiply__", output);
|
||||||
|
if (!__multiply__) {
|
||||||
|
ArgonObject *cls___name__ = get_field(output, "__name__", true, false);
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"Object '%.*s' is missing __multiply__ method",
|
||||||
|
(int)cls___name__->value.as_str.length,
|
||||||
|
cls___name__->value.as_str.data);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
output =
|
||||||
|
argon_call(__multiply__, 1, (ArgonObject *[]){argv[i]}, err, state);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArgonObject *ARGON_DIVISION_FUNCTION(size_t argc, ArgonObject **argv,
|
||||||
|
ArErr *err, RuntimeState *state) {
|
||||||
|
if (argc < 1) {
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"division expects at least 1 argument, got %" PRIu64, argc);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
ArgonObject *output = argv[0];
|
||||||
|
for (size_t i = 1; i < argc; i++) {
|
||||||
|
ArgonObject *__multiply__ = get_field_for_class(
|
||||||
|
get_field(output, "__class__", false, false), "__division__", output);
|
||||||
|
if (!__multiply__) {
|
||||||
|
ArgonObject *cls___name__ = get_field(output, "__name__", true, false);
|
||||||
|
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||||
|
"Object '%.*s' is missing __division__ method",
|
||||||
|
(int)cls___name__->value.as_str.length,
|
||||||
|
cls___name__->value.as_str.data);
|
||||||
|
return ARGON_NULL;
|
||||||
|
}
|
||||||
|
output =
|
||||||
|
argon_call(__multiply__, 1, (ArgonObject *[]){argv[i]}, err, state);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
ArgonObject *ARGON_TYPE_TYPE___call__(size_t argc, ArgonObject **argv,
|
ArgonObject *ARGON_TYPE_TYPE___call__(size_t argc, ArgonObject **argv,
|
||||||
ArErr *err, RuntimeState *state) {
|
ArErr *err, RuntimeState *state) {
|
||||||
(void)state;
|
(void)state;
|
||||||
@@ -509,6 +559,10 @@ void bootstrap_types() {
|
|||||||
create_argon_native_function("add", ARGON_ADDITION_FUNCTION);
|
create_argon_native_function("add", ARGON_ADDITION_FUNCTION);
|
||||||
SUBTRACTION_FUNCTION =
|
SUBTRACTION_FUNCTION =
|
||||||
create_argon_native_function("subtract", ARGON_SUBTRACTION_FUNCTION);
|
create_argon_native_function("subtract", ARGON_SUBTRACTION_FUNCTION);
|
||||||
|
MULTIPLY_FUNCTION =
|
||||||
|
create_argon_native_function("multiply", ARGON_MULTIPLY_FUNCTION);
|
||||||
|
DIVISION_FUNCTION =
|
||||||
|
create_argon_native_function("division", ARGON_DIVISION_FUNCTION);
|
||||||
add_field(BASE_CLASS, "__get_attr__", ACCESS_FUNCTION);
|
add_field(BASE_CLASS, "__get_attr__", ACCESS_FUNCTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -527,6 +581,8 @@ void bootstrap_globals() {
|
|||||||
add_to_scope(Global_Scope, "number", ARGON_NUMBER_TYPE);
|
add_to_scope(Global_Scope, "number", ARGON_NUMBER_TYPE);
|
||||||
add_to_scope(Global_Scope, "add", ADDITION_FUNCTION);
|
add_to_scope(Global_Scope, "add", ADDITION_FUNCTION);
|
||||||
add_to_scope(Global_Scope, "subtract", SUBTRACTION_FUNCTION);
|
add_to_scope(Global_Scope, "subtract", SUBTRACTION_FUNCTION);
|
||||||
|
add_to_scope(Global_Scope, "multiply", MULTIPLY_FUNCTION);
|
||||||
|
add_to_scope(Global_Scope, "division", DIVISION_FUNCTION);
|
||||||
|
|
||||||
ArgonObject *argon_term = new_object();
|
ArgonObject *argon_term = new_object();
|
||||||
add_field(argon_term, "__init__", ARGON_NULL);
|
add_field(argon_term, "__init__", ARGON_NULL);
|
||||||
@@ -722,6 +778,12 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
|||||||
case OP_LOAD_SUBTRACTION_FUNCTION:
|
case OP_LOAD_SUBTRACTION_FUNCTION:
|
||||||
state->registers[0] = SUBTRACTION_FUNCTION;
|
state->registers[0] = SUBTRACTION_FUNCTION;
|
||||||
break;
|
break;
|
||||||
|
case OP_LOAD_MULTIPLY_FUNCTION:
|
||||||
|
state->registers[0] = MULTIPLY_FUNCTION;
|
||||||
|
break;
|
||||||
|
case OP_LOAD_DIVISION_FUNCTION:
|
||||||
|
state->registers[0] = DIVISION_FUNCTION;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x",
|
return create_err(0, 0, 0, NULL, "Runtime Error", "Invalid Opcode %#x",
|
||||||
opcode);
|
opcode);
|
||||||
|
|||||||
273
src/shell.c
Normal file
273
src/shell.c
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2025 William Bell
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
#include "./lexer/lexer.h"
|
||||||
|
#include "./runtime/call/call.h"
|
||||||
|
#include "./runtime/objects/functions/functions.h"
|
||||||
|
#include "./runtime/objects/term/term.h"
|
||||||
|
#include "./runtime/runtime.h"
|
||||||
|
#include "./translator/translator.h"
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#if defined(__linux__)
|
||||||
|
#include <malloc.h>
|
||||||
|
#endif
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
FILE *fmemopen(void *buf, size_t size, const char *mode) {
|
||||||
|
if (strchr(mode, 'r') == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *tmp = tmpfile();
|
||||||
|
if (!tmp)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (fwrite(buf, 1, size, tmp) != size) {
|
||||||
|
fclose(tmp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
rewind(tmp);
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#include "../external/linenoise/linenoise.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Ctrl+C handler
|
||||||
|
void handle_sigint(int sig) {
|
||||||
|
(void)sig;
|
||||||
|
printf("\nBye :)\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int execute_code(FILE *stream, char *path, Stack *scope,
|
||||||
|
RuntimeState *runtime_state) {
|
||||||
|
if (!stream) {
|
||||||
|
perror("fmemopen");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArErr err;
|
||||||
|
|
||||||
|
DArray tokens;
|
||||||
|
darray_init(&tokens, sizeof(Token));
|
||||||
|
LexerState state = {path, stream, 0, 0, &tokens};
|
||||||
|
err = lexer(state);
|
||||||
|
if (err.exists) {
|
||||||
|
darray_free(&tokens, free_token);
|
||||||
|
output_err(err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
DArray ast;
|
||||||
|
|
||||||
|
darray_init(&ast, sizeof(ParsedValue));
|
||||||
|
|
||||||
|
err = parser(path, &ast, &tokens, false);
|
||||||
|
darray_free(&tokens, free_token);
|
||||||
|
if (err.exists) {
|
||||||
|
darray_free(&ast, free_parsed);
|
||||||
|
output_err(err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
Translated __translated = init_translator(path);
|
||||||
|
err = translate(&__translated, &ast);
|
||||||
|
darray_free(&ast, free_parsed);
|
||||||
|
if (err.exists) {
|
||||||
|
darray_free(&__translated.bytecode, NULL);
|
||||||
|
free(__translated.constants.data);
|
||||||
|
hashmap_free(__translated.constants.hashmap, NULL);
|
||||||
|
output_err(err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
hashmap_free(__translated.constants.hashmap, NULL);
|
||||||
|
Translated translated = {
|
||||||
|
__translated.registerCount, NULL, {}, {}, __translated.path};
|
||||||
|
translated.bytecode.data = ar_alloc(__translated.bytecode.capacity);
|
||||||
|
memcpy(translated.bytecode.data, __translated.bytecode.data,
|
||||||
|
__translated.bytecode.capacity);
|
||||||
|
translated.bytecode.element_size = __translated.bytecode.element_size;
|
||||||
|
translated.bytecode.size = __translated.bytecode.size;
|
||||||
|
translated.bytecode.resizable = false;
|
||||||
|
translated.bytecode.capacity =
|
||||||
|
__translated.bytecode.size * __translated.bytecode.element_size;
|
||||||
|
translated.constants.data = ar_alloc(__translated.constants.capacity);
|
||||||
|
memcpy(translated.constants.data, __translated.constants.data,
|
||||||
|
__translated.constants.capacity);
|
||||||
|
translated.constants.size = __translated.constants.size;
|
||||||
|
translated.constants.capacity = __translated.constants.capacity;
|
||||||
|
darray_free(&__translated.bytecode, NULL);
|
||||||
|
free(__translated.constants.data);
|
||||||
|
*runtime_state = init_runtime_state(translated, path);
|
||||||
|
err = runtime(translated, *runtime_state, scope);
|
||||||
|
if (err.exists) {
|
||||||
|
output_err(err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple input function
|
||||||
|
char *input(const char *prompt) {
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
printf("%s", prompt);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
char *buffer = NULL;
|
||||||
|
size_t size = 0;
|
||||||
|
ssize_t len = getline(&buffer, &size, stdin);
|
||||||
|
|
||||||
|
if (len == -1) {
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len > 0 && buffer[len - 1] == '\n') {
|
||||||
|
buffer[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
char *buffer = linenoise(prompt);
|
||||||
|
if (buffer && buffer[0] != '\0') {
|
||||||
|
linenoiseHistoryAdd(buffer);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *read_all_stdin(size_t *out_len) {
|
||||||
|
size_t size = 1024;
|
||||||
|
size_t len = 0;
|
||||||
|
char *buffer = malloc(size);
|
||||||
|
if (!buffer)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
int c;
|
||||||
|
while ((c = fgetc(stdin)) != EOF) {
|
||||||
|
if (len + 1 >= size) {
|
||||||
|
size *= 2;
|
||||||
|
buffer = realloc(buffer, size);
|
||||||
|
if (!buffer)
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
buffer[len++] = (char)c;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_len = len;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int shell() {
|
||||||
|
|
||||||
|
Stack *main_scope = create_scope(Global_Scope);
|
||||||
|
|
||||||
|
if (!isatty(STDIN_FILENO)) {
|
||||||
|
RuntimeState runtime_state;
|
||||||
|
size_t len;
|
||||||
|
char *data = read_all_stdin(&len);
|
||||||
|
FILE *file = fmemopen(data, len, "r");
|
||||||
|
int resp = execute_code(file, "<stdin>", main_scope, &runtime_state);
|
||||||
|
fclose(file);
|
||||||
|
free(data);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
signal(SIGINT, handle_sigint);
|
||||||
|
|
||||||
|
printf("Welcome to the Argon shell!\n\n");
|
||||||
|
|
||||||
|
ArgonObject *output_object = create_argon_native_function("log", term_log);
|
||||||
|
char *totranslate = NULL;
|
||||||
|
size_t totranslatelength = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
#if defined(__linux__)
|
||||||
|
malloc_trim(0);
|
||||||
|
#endif
|
||||||
|
if (totranslate) {
|
||||||
|
free(totranslate);
|
||||||
|
totranslate = NULL;
|
||||||
|
totranslatelength = 0;
|
||||||
|
};
|
||||||
|
int indent = 0;
|
||||||
|
char textBefore[] = ">>> ";
|
||||||
|
|
||||||
|
// Dynamic array of lines
|
||||||
|
|
||||||
|
do {
|
||||||
|
// indent string
|
||||||
|
size_t isz = (size_t)indent * 4;
|
||||||
|
char *indentStr = (char *)malloc(isz + 1);
|
||||||
|
if (!indentStr)
|
||||||
|
exit(1);
|
||||||
|
memset(indentStr, ' ', isz);
|
||||||
|
indentStr[isz] = '\0';
|
||||||
|
|
||||||
|
// prompt
|
||||||
|
size_t p_len = strlen(textBefore) + isz;
|
||||||
|
char *prompt = (char *)malloc(p_len + 1);
|
||||||
|
if (!prompt)
|
||||||
|
exit(1);
|
||||||
|
memcpy(prompt, textBefore, strlen(textBefore));
|
||||||
|
memcpy(prompt + strlen(textBefore), indentStr, isz + 1);
|
||||||
|
|
||||||
|
char *inp = input(prompt);
|
||||||
|
free(prompt);
|
||||||
|
|
||||||
|
if (!inp) {
|
||||||
|
printf("\nBye :)\n");
|
||||||
|
// Free previously collected lines
|
||||||
|
free(inp);
|
||||||
|
free(totranslate);
|
||||||
|
free(indentStr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append line to totranslate
|
||||||
|
size_t length = strlen(inp);
|
||||||
|
totranslate = realloc(totranslate, totranslatelength + isz + length + 1);
|
||||||
|
memcpy(totranslate + totranslatelength, indentStr, isz);
|
||||||
|
memcpy(totranslate + totranslatelength + isz, inp, length);
|
||||||
|
totranslatelength += isz + length + 1;
|
||||||
|
totranslate[totranslatelength - 1] = '\n';
|
||||||
|
|
||||||
|
char *trimmed = inp;
|
||||||
|
while (*trimmed == ' ' || *trimmed == '\t')
|
||||||
|
trimmed++;
|
||||||
|
|
||||||
|
size_t len = strlen(trimmed);
|
||||||
|
if (len >= 2 && strcmp(trimmed + len - 2, "do") == 0) {
|
||||||
|
indent++;
|
||||||
|
} else if (len == 0 && indent > 0) {
|
||||||
|
indent--;
|
||||||
|
}
|
||||||
|
free(inp);
|
||||||
|
strcpy(textBefore, "... ");
|
||||||
|
free(indentStr);
|
||||||
|
|
||||||
|
} while (indent > 0);
|
||||||
|
totranslate = realloc(totranslate, totranslatelength + 1);
|
||||||
|
totranslate[totranslatelength] = '\0';
|
||||||
|
RuntimeState runtime_state;
|
||||||
|
FILE *file = fmemopen((void *)totranslate, totranslatelength, "r");
|
||||||
|
int resp = execute_code(file, "<shell>", main_scope, &runtime_state);
|
||||||
|
fclose(file);
|
||||||
|
if (resp) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ArErr err;
|
||||||
|
argon_call(output_object, 1, (ArgonObject *[]){runtime_state.registers[0]},
|
||||||
|
&err, &runtime_state);
|
||||||
|
totranslatelength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
src/shell.h
Normal file
12
src/shell.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2025 William Bell
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ARGON_SHELL_H
|
||||||
|
#define ARGON_SHELL_H
|
||||||
|
|
||||||
|
int shell();
|
||||||
|
|
||||||
|
#endif // ARGON_SHELL_H
|
||||||
@@ -10,19 +10,22 @@ size_t translate_access(Translated *translated, ParsedAccess *access,
|
|||||||
set_registers(translated, 1);
|
set_registers(translated, 1);
|
||||||
uint64_t first = push_instruction_byte(translated, OP_LOAD_ACCESS_FUNCTION);
|
uint64_t first = push_instruction_byte(translated, OP_LOAD_ACCESS_FUNCTION);
|
||||||
push_instruction_byte(translated, OP_INIT_CALL);
|
push_instruction_byte(translated, OP_INIT_CALL);
|
||||||
push_instruction_code(translated, 3);
|
push_instruction_code(translated, access->access.size + 2);
|
||||||
|
|
||||||
translate_parsed(translated, &access->to_access, err);
|
translate_parsed(translated, &access->to_access, err);
|
||||||
push_instruction_byte(translated, OP_INSERT_ARG);
|
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||||
push_instruction_code(translated, 0);
|
push_instruction_code(translated, 0);
|
||||||
translate_parsed(translated, darray_get(&access->access, 0), err);
|
|
||||||
push_instruction_byte(translated, OP_INSERT_ARG);
|
|
||||||
push_instruction_code(translated, 1);
|
|
||||||
|
|
||||||
push_instruction_byte(translated, OP_LOAD_BOOL);
|
push_instruction_byte(translated, OP_LOAD_BOOL);
|
||||||
push_instruction_byte(translated, access->access_fields);
|
push_instruction_byte(translated, access->access_fields);
|
||||||
push_instruction_byte(translated, OP_INSERT_ARG);
|
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||||
push_instruction_code(translated, 2);
|
push_instruction_code(translated, 1);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < access->access.size; i++) {
|
||||||
|
translate_parsed(translated, darray_get(&access->access, i), err);
|
||||||
|
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||||
|
push_instruction_code(translated, 2+i);
|
||||||
|
}
|
||||||
|
|
||||||
push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
||||||
push_instruction_code(translated, access->line);
|
push_instruction_code(translated, access->line);
|
||||||
push_instruction_code(translated, access->column);
|
push_instruction_code(translated, access->column);
|
||||||
|
|||||||
@@ -136,3 +136,11 @@ loads the addition function into register 1
|
|||||||
## OP_LOAD_SUBTRACTION_FUNCTION
|
## OP_LOAD_SUBTRACTION_FUNCTION
|
||||||
|
|
||||||
loads the subtraction function into register 1
|
loads the subtraction function into register 1
|
||||||
|
|
||||||
|
## OP_LOAD_MULTIPLY_FUNCTION
|
||||||
|
|
||||||
|
loads the multiply function into register 1
|
||||||
|
|
||||||
|
## OP_LOAD_DIVISION_FUNCTION
|
||||||
|
|
||||||
|
loads the division function into register 1
|
||||||
@@ -18,6 +18,12 @@ size_t translate_operation(Translated *translated, ParsedOperation *operation,
|
|||||||
case TOKEN_MINUS:
|
case TOKEN_MINUS:
|
||||||
first = push_instruction_byte(translated, OP_LOAD_SUBTRACTION_FUNCTION);
|
first = push_instruction_byte(translated, OP_LOAD_SUBTRACTION_FUNCTION);
|
||||||
break;
|
break;
|
||||||
|
case TOKEN_STAR:
|
||||||
|
first = push_instruction_byte(translated, OP_LOAD_MULTIPLY_FUNCTION);
|
||||||
|
break;
|
||||||
|
case TOKEN_SLASH:
|
||||||
|
first = push_instruction_byte(translated, OP_LOAD_DIVISION_FUNCTION);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
*err = create_err(operation->line, operation->column, operation->length,
|
*err = create_err(operation->line, operation->column, operation->length,
|
||||||
translated->path, "Syntax Error", "unknown operation");
|
translated->path, "Syntax Error", "unknown operation");
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ typedef enum {
|
|||||||
OP_LOAD_BOOL,
|
OP_LOAD_BOOL,
|
||||||
OP_LOAD_NUMBER,
|
OP_LOAD_NUMBER,
|
||||||
OP_LOAD_ADDITION_FUNCTION,
|
OP_LOAD_ADDITION_FUNCTION,
|
||||||
OP_LOAD_SUBTRACTION_FUNCTION
|
OP_LOAD_SUBTRACTION_FUNCTION,
|
||||||
|
OP_LOAD_MULTIPLY_FUNCTION,
|
||||||
|
OP_LOAD_DIVISION_FUNCTION
|
||||||
} OperationType;
|
} OperationType;
|
||||||
|
|
||||||
void arena_resize(ConstantArena *arena, size_t new_size);
|
void arena_resize(ConstantArena *arena, size_t new_size);
|
||||||
|
|||||||
@@ -8,3 +8,8 @@ let say_hi(name) = do
|
|||||||
return "hello "+u+", how are you?"
|
return "hello "+u+", how are you?"
|
||||||
|
|
||||||
term.log(say_hi("william"))
|
term.log(say_hi("william"))
|
||||||
|
|
||||||
|
let a = 9
|
||||||
|
let b = 10
|
||||||
|
|
||||||
|
term.log(string(a)+'+'+string(b)+'='+string(a+b))
|
||||||
12
windows-profile.txt
Normal file
12
windows-profile.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[settings]
|
||||||
|
os=Windows
|
||||||
|
compiler=gcc
|
||||||
|
compiler.version=12
|
||||||
|
compiler.libcxx=libstdc++11
|
||||||
|
compiler.threads=posix
|
||||||
|
compiler.exception=seh
|
||||||
|
arch=x86_64
|
||||||
|
build_type=Release
|
||||||
|
|
||||||
|
[tool_requires]
|
||||||
|
mingw-builds/12.2.0
|
||||||
Reference in New Issue
Block a user