Skip to content

Commit

Permalink
tests galore + version bump + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ImShyMike committed Jan 18, 2025
1 parent f2b3f06 commit 130d589
Show file tree
Hide file tree
Showing 59 changed files with 1,829 additions and 19 deletions.
5 changes: 4 additions & 1 deletion docs/docs/language-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ There are also many builtin functions:
* **range(** start, end?, step? **)**: Generates an array from start to end with step
There are also many builtin modules:
There are also many builtin modules.
They can be imported using `import` (without ".eryx")
Full list:
- **time**:
- **time()**: Get the current time in seconds since the Epoch
Expand Down
2 changes: 1 addition & 1 deletion eryx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of the package."""

CURRENT_VERSION = "0.3.9"
CURRENT_VERSION = "0.3.10"
4 changes: 2 additions & 2 deletions eryx/frontend/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def tokenize(source_code: str) -> list[Token]:

if src[0] == "&":
src.pop(0)
if len(src) > 1 and src[1] == "&":
if len(src) > 0 and src[0] == "&":
src.pop(0)
tokens.append(Token("&&", TokenType.BINARY_OPERATOR, current_pos))
else:
Expand All @@ -181,7 +181,7 @@ def tokenize(source_code: str) -> list[Token]:

if src[0] == "|":
src.pop(0)
if len(src) > 1 and src[1] == "|":
if len(src) > 0 and src[0] == "|":
src.pop(0)
tokens.append(Token("||", TokenType.BINARY_OPERATOR, current_pos))
else:
Expand Down
18 changes: 11 additions & 7 deletions eryx/runtime/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ def declare_variable(

return value

def assign_variable(self, variable_name: str, value: RuntimeValue) -> RuntimeValue:
def assign_variable(
self, variable_name: str, value: RuntimeValue, overwrite: bool = False
) -> RuntimeValue:
"""Assign a value to a variable in the current scope."""
environment = self.resolve(variable_name)

if variable_name in environment.constants:
if variable_name in environment.constants and not overwrite:
raise RuntimeError(f'Cannot assign to constant variable "{variable_name}"')

environment.variables[variable_name] = value
Expand Down Expand Up @@ -194,9 +196,11 @@ def _range(args: list[RuntimeValue], _: Environment) -> RuntimeValue:
if len(args) == 2:
if all(isinstance(i, NumberValue) for i in args):
return ArrayValue(
[NumberValue(i) for i in range(
int(args[0].value), # type: ignore
int(args[1].value), # type: ignore
[
NumberValue(i)
for i in range(
int(args[0].value), # type: ignore
int(args[1].value), # type: ignore
)
]
)
Expand All @@ -206,8 +210,8 @@ def _range(args: list[RuntimeValue], _: Environment) -> RuntimeValue:
[
NumberValue(i)
for i in range(
int(args[0].value), # type: ignore
int(args[1].value), # type: ignore
int(args[0].value), # type: ignore
int(args[1].value), # type: ignore
int(args[2].value), # type: ignore
)
]
Expand Down
12 changes: 12 additions & 0 deletions eryx/runtime/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ def eval_for_statement(
iterator = evaluate(for_statement.iterator, environment)
if not isinstance(iterator, ArrayValue):
raise RuntimeError("Expected an array as an iterator.")

variable_value = None
try:
variable_value = environment.lookup_variable(for_statement.variable.symbol)
except RuntimeError:
pass

for element in iterator.elements:
environment.declare_variable(for_statement.variable.symbol, element, False, True)
try:
Expand All @@ -275,6 +282,11 @@ def eval_for_statement(
except BreakException:
pass

if variable_value:
environment.assign_variable(for_statement.variable.symbol, variable_value, overwrite=True)
else:
environment.delete_variable(for_statement.variable.symbol)

return NullValue()

def eval_while_statement(
Expand Down
29 changes: 26 additions & 3 deletions eryx/tests/make_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tkinter as tk
from io import StringIO
from tkinter import messagebox
from tkinter.filedialog import askdirectory
from tkinter.filedialog import askdirectory, askopenfilename

from eryx.__init__ import CURRENT_VERSION
from eryx.frontend.parser import Parser
Expand Down Expand Up @@ -133,14 +133,37 @@ def on_remake_test():
messagebox.showinfo("Success", f"Test files for {info["name"]} have been regenerated.")


def on_make_test():
"""Handle making a test from a file."""
test_file = askopenfilename(
initialdir=os.path.join(current_path, "test"),
title="Select File",
filetypes=[("Eryx Files", "*.eryx")],
)

if not test_file:
return

with open(test_file, "r", encoding="utf8") as f:
code = f.read()

test_name = os.path.basename(test_file).replace(".eryx", "")
description = ""

create_test_files(code, description, test_name)

# Set up the Tkinter GUI
root = tk.Tk()
root.title("Test File Generator")
root.resizable(False, False)

# Remake test
remake_label = tk.Button(root, text="Remake Test", command=on_remake_test)
remake_label.pack()
remake_button = tk.Button(root, text="Remake Test", command=on_remake_test)
remake_button.pack()

# Make test from file
make_test_button = tk.Button(root, text="From File", command=on_make_test)
make_test_button.pack()

# Code input
code_label = tk.Label(root, text="Code:")
Expand Down
6 changes: 6 additions & 0 deletions eryx/tests/test/builtins/builtins.eryx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "math";
import "time";
from "file" import ["read"];
import "http" as "client";

print(math.min([1, 2]));
61 changes: 61 additions & 0 deletions eryx/tests/test/builtins/builtins.eryx.ast

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eryx/tests/test/builtins/builtins.eryx.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions eryx/tests/test/builtins/test.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 0.3.9
Name: builtins
Description:
2 changes: 2 additions & 0 deletions eryx/tests/test/comments/comments.eryx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is just a comment
# this is not just a comment ; print("this will be printed")
14 changes: 14 additions & 0 deletions eryx/tests/test/comments/comments.eryx.ast

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eryx/tests/test/comments/comments.eryx.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions eryx/tests/test/comments/test.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 0.3.9
Name: comments
Description:
2 changes: 1 addition & 1 deletion eryx/tests/test/concat/test.info
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.3.8
Version: 0.3.9
Name: concat
Description: String, array and object concatenation
16 changes: 16 additions & 0 deletions eryx/tests/test/for_loops/for_loops.eryx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const item = 4000;

for item in [1, 2, 3, 4, 5] {
print(item);
}

for item in range(1, 6) {
print(item);
}

const lst = [1, 2, 3, 4, 5];
for item in lst {
print(item);
}

print(item);
Loading

0 comments on commit 130d589

Please sign in to comment.