@lewin671/python-vm 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js.map +1 -0
- package/dist/compiler_module/compiler.d.ts.map +1 -0
- package/dist/compiler_module/compiler.js.map +1 -0
- package/dist/compiler_module/index.d.ts.map +1 -0
- package/dist/compiler_module/index.js.map +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/lexer/index.d.ts.map +1 -0
- package/dist/lexer/index.js.map +1 -0
- package/dist/lexer/lexer.d.ts.map +1 -0
- package/dist/lexer/lexer.js.map +1 -0
- package/dist/parser/expressions.d.ts.map +1 -0
- package/dist/parser/expressions.js.map +1 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/targets.d.ts.map +1 -0
- package/dist/parser/targets.js.map +1 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js.map +1 -0
- package/dist/types/bytecode.d.ts.map +1 -0
- package/dist/types/bytecode.js.map +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/token.d.ts.map +1 -0
- package/dist/types/token.js.map +1 -0
- package/dist/vm/builtins.d.ts.map +1 -0
- package/dist/vm/builtins.js.map +1 -0
- package/dist/vm/callable.d.ts.map +1 -0
- package/dist/vm/callable.js.map +1 -0
- package/dist/vm/execution.d.ts.map +1 -0
- package/dist/vm/execution.js.map +1 -0
- package/dist/vm/expression-generator.d.ts.map +1 -0
- package/dist/vm/expression-generator.js.map +1 -0
- package/dist/vm/expressions.d.ts.map +1 -0
- package/dist/vm/expressions.js.map +1 -0
- package/dist/vm/imports.d.ts.map +1 -0
- package/dist/vm/imports.js.map +1 -0
- package/dist/vm/index.d.ts.map +1 -0
- package/dist/vm/index.js.map +1 -0
- package/dist/vm/operations.d.ts.map +1 -0
- package/dist/vm/operations.js.map +1 -0
- package/dist/vm/runtime-types.d.ts.map +1 -0
- package/dist/vm/runtime-types.js.map +1 -0
- package/dist/vm/statements.d.ts.map +1 -0
- package/dist/vm/statements.js.map +1 -0
- package/dist/vm/truthy.d.ts.map +1 -0
- package/dist/vm/truthy.js.map +1 -0
- package/dist/vm/value-utils.d.ts.map +1 -0
- package/dist/vm/value-utils.js.map +1 -0
- package/dist/vm/vm.d.ts.map +1 -0
- package/dist/vm/vm.js.map +1 -0
- package/package.json +7 -1
- package/.claude/settings.local.json +0 -3
- package/.prettierrc +0 -7
- package/Agents.md +0 -66
- package/SETUP.md +0 -171
- package/examples/assert_testing.py +0 -38
- package/examples/big_int_precision.py +0 -2
- package/examples/boolean_logic.py +0 -35
- package/examples/break_continue.py +0 -43
- package/examples/classes_objects.py +0 -43
- package/examples/compiler_killer_async.py +0 -6
- package/examples/compiler_killer_bigint.py +0 -3
- package/examples/compiler_killer_bool_int_dict_key.py +0 -5
- package/examples/compiler_killer_bool_len.py +0 -9
- package/examples/compiler_killer_floor_division.py +0 -4
- package/examples/compiler_killer_is_identity.py +0 -3
- package/examples/compiler_killer_list_sort_return.py +0 -3
- package/examples/compiler_killer_match.py +0 -13
- package/examples/compiler_killer_negative_repeat.py +0 -3
- package/examples/compiler_killer_negative_zero_repr.py +0 -3
- package/examples/compiler_killer_rounding.py +0 -4
- package/examples/compiler_killer_slice_assign.py +0 -3
- package/examples/comprehensions.py +0 -28
- package/examples/conditions.py +0 -13
- package/examples/context_manager.py +0 -35
- package/examples/decorators.py +0 -50
- package/examples/exceptions.py +0 -40
- package/examples/fibonacci.py +0 -10
- package/examples/functions.py +0 -38
- package/examples/generator.py +0 -51
- package/examples/global_nonlocal.py +0 -48
- package/examples/hello.py +0 -3
- package/examples/itertools_example.py +0 -33
- package/examples/lists_dicts.py +0 -29
- package/examples/loops.py +0 -19
- package/examples/math_ops.py +0 -15
- package/examples/nan_set.py +0 -6
- package/examples/numbers_operators.py +0 -51
- package/examples/sets.py +0 -36
- package/examples/slicing.py +0 -29
- package/examples/starred_unpacking.py +0 -3
- package/examples/string_formatting.py +0 -36
- package/examples/strings.py +0 -22
- package/examples/tuples.py +0 -45
- package/examples/type_conversion.py +0 -41
- package/jest.config.js +0 -15
- package/notes/iterations/compiler-runtime/compiler-runtime_2025-09-16.md +0 -25
- package/notes/iterations/compiler-runtime/compiler-runtime_2026-01-16.md +0 -24
- package/notes/iterations/compiler-runtime/compiler-runtime_test_2026-01-16.md +0 -21
- package/notes/iterations/floor-division/floor-division_2026-01-16.md +0 -29
- package/prompts/commit.txt +0 -9
- package/prompts/task.txt +0 -21
- package/prompts/test.txt +0 -23
- package/scripts/codex-loop.js +0 -215
- package/scripts/verify.sh +0 -12
- package/src/compiler.ts +0 -58
- package/src/compiler_module/compiler.ts +0 -19
- package/src/compiler_module/index.ts +0 -1
- package/src/index.ts +0 -39
- package/src/lexer/index.ts +0 -1
- package/src/lexer/lexer.ts +0 -402
- package/src/parser/expressions.ts +0 -462
- package/src/parser/index.ts +0 -1
- package/src/parser/parser.ts +0 -102
- package/src/parser/statements.ts +0 -366
- package/src/parser/targets.ts +0 -71
- package/src/types/ast.ts +0 -64
- package/src/types/bytecode.ts +0 -50
- package/src/types/index.ts +0 -3
- package/src/types/token.ts +0 -44
- package/src/vm/builtins.ts +0 -237
- package/src/vm/callable.ts +0 -154
- package/src/vm/execution.ts +0 -251
- package/src/vm/expression-generator.ts +0 -65
- package/src/vm/expressions.ts +0 -373
- package/src/vm/imports.ts +0 -61
- package/src/vm/index.ts +0 -2
- package/src/vm/operations.ts +0 -414
- package/src/vm/runtime-types.ts +0 -292
- package/src/vm/statements.ts +0 -358
- package/src/vm/truthy.ts +0 -36
- package/src/vm/value-utils.ts +0 -173
- package/src/vm/vm.ts +0 -80
- package/tests/compiler.test.ts +0 -111
- package/tsconfig.json +0 -20
- package/vitest.config.ts +0 -16
package/examples/decorators.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# 装饰器示例
|
|
2
|
-
def simple_decorator(func):
|
|
3
|
-
def wrapper():
|
|
4
|
-
print("Something before function")
|
|
5
|
-
func()
|
|
6
|
-
print("Something after function")
|
|
7
|
-
return wrapper
|
|
8
|
-
|
|
9
|
-
@simple_decorator
|
|
10
|
-
def say_hello():
|
|
11
|
-
print("Hello!")
|
|
12
|
-
|
|
13
|
-
say_hello()
|
|
14
|
-
|
|
15
|
-
# 带参数的装饰器
|
|
16
|
-
def decorator_with_args(func):
|
|
17
|
-
def wrapper(*args, **kwargs):
|
|
18
|
-
print(f"Calling {func.__name__} with args {args}")
|
|
19
|
-
result = func(*args, **kwargs)
|
|
20
|
-
print(f"Result: {result}")
|
|
21
|
-
return result
|
|
22
|
-
return wrapper
|
|
23
|
-
|
|
24
|
-
@decorator_with_args
|
|
25
|
-
def add(a, b):
|
|
26
|
-
return a + b
|
|
27
|
-
|
|
28
|
-
result = add(5, 3)
|
|
29
|
-
|
|
30
|
-
# 多个装饰器
|
|
31
|
-
def decorator1(func):
|
|
32
|
-
def wrapper():
|
|
33
|
-
print("Decorator 1 before")
|
|
34
|
-
func()
|
|
35
|
-
print("Decorator 1 after")
|
|
36
|
-
return wrapper
|
|
37
|
-
|
|
38
|
-
def decorator2(func):
|
|
39
|
-
def wrapper():
|
|
40
|
-
print("Decorator 2 before")
|
|
41
|
-
func()
|
|
42
|
-
print("Decorator 2 after")
|
|
43
|
-
return wrapper
|
|
44
|
-
|
|
45
|
-
@decorator1
|
|
46
|
-
@decorator2
|
|
47
|
-
def greet():
|
|
48
|
-
print("Hello!")
|
|
49
|
-
|
|
50
|
-
greet()
|
package/examples/exceptions.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# 异常处理
|
|
2
|
-
try:
|
|
3
|
-
x = 10
|
|
4
|
-
y = 0
|
|
5
|
-
result = x / y
|
|
6
|
-
except ZeroDivisionError:
|
|
7
|
-
print("Error: Cannot divide by zero")
|
|
8
|
-
|
|
9
|
-
# 多个异常
|
|
10
|
-
try:
|
|
11
|
-
num = int("abc")
|
|
12
|
-
except ValueError:
|
|
13
|
-
print("Error: Invalid integer")
|
|
14
|
-
except TypeError:
|
|
15
|
-
print("Error: Type error")
|
|
16
|
-
|
|
17
|
-
# Try-except-else
|
|
18
|
-
try:
|
|
19
|
-
result = 10 / 2
|
|
20
|
-
except ZeroDivisionError:
|
|
21
|
-
print("Cannot divide by zero")
|
|
22
|
-
else:
|
|
23
|
-
print(f"Result: {result}")
|
|
24
|
-
|
|
25
|
-
# Try-finally
|
|
26
|
-
try:
|
|
27
|
-
file = open("nonexistent.txt", "r")
|
|
28
|
-
except FileNotFoundError:
|
|
29
|
-
print("File not found")
|
|
30
|
-
finally:
|
|
31
|
-
print("This always executes")
|
|
32
|
-
|
|
33
|
-
# 自定义异常
|
|
34
|
-
class CustomError(Exception):
|
|
35
|
-
pass
|
|
36
|
-
|
|
37
|
-
try:
|
|
38
|
-
raise CustomError("This is a custom error")
|
|
39
|
-
except CustomError as e:
|
|
40
|
-
print(f"Caught: {e}")
|
package/examples/fibonacci.py
DELETED
package/examples/functions.py
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# 函数定义和调用
|
|
2
|
-
def greet(name):
|
|
3
|
-
return f"Hello, {name}!"
|
|
4
|
-
|
|
5
|
-
print(greet("Alice"))
|
|
6
|
-
print(greet("Bob"))
|
|
7
|
-
|
|
8
|
-
# 默认参数
|
|
9
|
-
def add(a, b=10):
|
|
10
|
-
return a + b
|
|
11
|
-
|
|
12
|
-
print("add(5):", add(5))
|
|
13
|
-
print("add(5, 15):", add(5, 15))
|
|
14
|
-
|
|
15
|
-
# 可变参数
|
|
16
|
-
def sum_numbers(*args):
|
|
17
|
-
total = 0
|
|
18
|
-
for num in args:
|
|
19
|
-
total += num
|
|
20
|
-
return total
|
|
21
|
-
|
|
22
|
-
print("sum(1, 2, 3):", sum_numbers(1, 2, 3))
|
|
23
|
-
print("sum(5, 10):", sum_numbers(5, 10))
|
|
24
|
-
|
|
25
|
-
# 关键字参数
|
|
26
|
-
def print_info(**kwargs):
|
|
27
|
-
for key, value in kwargs.items():
|
|
28
|
-
print(f"{key}: {value}")
|
|
29
|
-
|
|
30
|
-
print_info(name="Charlie", age=25, city="Boston")
|
|
31
|
-
|
|
32
|
-
# Lambda 函数
|
|
33
|
-
square = lambda x: x * x
|
|
34
|
-
print("Lambda square(5):", square(5))
|
|
35
|
-
|
|
36
|
-
numbers = [1, 2, 3, 4, 5]
|
|
37
|
-
doubled = list(map(lambda x: x * 2, numbers))
|
|
38
|
-
print("Doubled:", doubled)
|
package/examples/generator.py
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# 生成器函数
|
|
2
|
-
def simple_generator():
|
|
3
|
-
yield 1
|
|
4
|
-
yield 2
|
|
5
|
-
yield 3
|
|
6
|
-
|
|
7
|
-
print("Simple generator:")
|
|
8
|
-
for value in simple_generator():
|
|
9
|
-
print(value)
|
|
10
|
-
|
|
11
|
-
# 斐波那契生成器
|
|
12
|
-
def fibonacci_gen(n):
|
|
13
|
-
a, b = 0, 1
|
|
14
|
-
count = 0
|
|
15
|
-
while count < n:
|
|
16
|
-
yield a
|
|
17
|
-
a, b = b, a + b
|
|
18
|
-
count += 1
|
|
19
|
-
|
|
20
|
-
print("\nFibonacci generator:")
|
|
21
|
-
for num in fibonacci_gen(10):
|
|
22
|
-
print(num, end=" ")
|
|
23
|
-
print()
|
|
24
|
-
|
|
25
|
-
# 使用 next() 函数
|
|
26
|
-
print("\nUsing next():")
|
|
27
|
-
gen = simple_generator()
|
|
28
|
-
print(next(gen))
|
|
29
|
-
print(next(gen))
|
|
30
|
-
print(next(gen))
|
|
31
|
-
|
|
32
|
-
# 生成器表达式
|
|
33
|
-
print("\nGenerator expression:")
|
|
34
|
-
gen_expr = (x**2 for x in range(5))
|
|
35
|
-
print(list(gen_expr))
|
|
36
|
-
|
|
37
|
-
# Send 方法
|
|
38
|
-
def counter_gen():
|
|
39
|
-
count = 0
|
|
40
|
-
while True:
|
|
41
|
-
x = yield count
|
|
42
|
-
if x is not None:
|
|
43
|
-
count = x
|
|
44
|
-
else:
|
|
45
|
-
count += 1
|
|
46
|
-
|
|
47
|
-
print("\nUsing send():")
|
|
48
|
-
gen = counter_gen()
|
|
49
|
-
print(next(gen))
|
|
50
|
-
print(gen.send(10))
|
|
51
|
-
print(next(gen))
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
# Global 和 Nonlocal 关键字
|
|
2
|
-
x = 10
|
|
3
|
-
|
|
4
|
-
def modify_global():
|
|
5
|
-
global x
|
|
6
|
-
x = 20
|
|
7
|
-
print(f"Inside function: x = {x}")
|
|
8
|
-
|
|
9
|
-
print(f"Before: x = {x}")
|
|
10
|
-
modify_global()
|
|
11
|
-
print(f"After: x = {x}")
|
|
12
|
-
|
|
13
|
-
# Nonlocal 关键字
|
|
14
|
-
def outer():
|
|
15
|
-
y = 10
|
|
16
|
-
print(f"Outer y: {y}")
|
|
17
|
-
|
|
18
|
-
def inner():
|
|
19
|
-
nonlocal y
|
|
20
|
-
y = 20
|
|
21
|
-
print(f"Inner y after modification: {y}")
|
|
22
|
-
|
|
23
|
-
inner()
|
|
24
|
-
print(f"Outer y after inner call: {y}")
|
|
25
|
-
|
|
26
|
-
outer()
|
|
27
|
-
|
|
28
|
-
# 闭包示例
|
|
29
|
-
def make_multiplier(n):
|
|
30
|
-
def multiplier(x):
|
|
31
|
-
return x * n
|
|
32
|
-
return multiplier
|
|
33
|
-
|
|
34
|
-
times3 = make_multiplier(3)
|
|
35
|
-
times5 = make_multiplier(5)
|
|
36
|
-
|
|
37
|
-
print(f"times3(10) = {times3(10)}")
|
|
38
|
-
print(f"times5(10) = {times5(10)}")
|
|
39
|
-
|
|
40
|
-
# 列表作为默认参数(陷阱)
|
|
41
|
-
def append_to_list(item, list_arg=[]):
|
|
42
|
-
list_arg.append(item)
|
|
43
|
-
return list_arg
|
|
44
|
-
|
|
45
|
-
print("\nDefault mutable argument:")
|
|
46
|
-
print(append_to_list(1))
|
|
47
|
-
print(append_to_list(2))
|
|
48
|
-
print(append_to_list(3))
|
package/examples/hello.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# Filter 和 Map 操作
|
|
2
|
-
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
3
|
-
|
|
4
|
-
# Filter
|
|
5
|
-
evens = list(filter(lambda x: x % 2 == 0, numbers))
|
|
6
|
-
print("Evens:", evens)
|
|
7
|
-
|
|
8
|
-
# Map
|
|
9
|
-
doubled = list(map(lambda x: x * 2, numbers))
|
|
10
|
-
print("Doubled:", doubled)
|
|
11
|
-
|
|
12
|
-
# Zip 操作
|
|
13
|
-
names = ["Alice", "Bob", "Charlie"]
|
|
14
|
-
ages = [25, 30, 35]
|
|
15
|
-
zipped = list(zip(names, ages))
|
|
16
|
-
print("Zipped:", zipped)
|
|
17
|
-
|
|
18
|
-
for name, age in zip(names, ages):
|
|
19
|
-
print(f"{name}: {age}")
|
|
20
|
-
|
|
21
|
-
# Enumerate
|
|
22
|
-
for i, name in enumerate(names):
|
|
23
|
-
print(f"{i}: {name}")
|
|
24
|
-
|
|
25
|
-
# Sorted 和 Reversed
|
|
26
|
-
data = [3, 1, 4, 1, 5, 9, 2, 6]
|
|
27
|
-
print("Sorted:", sorted(data))
|
|
28
|
-
print("Reversed:", list(reversed(data)))
|
|
29
|
-
|
|
30
|
-
# Range 操作
|
|
31
|
-
print("Range(5):", list(range(5)))
|
|
32
|
-
print("Range(2, 8):", list(range(2, 8)))
|
|
33
|
-
print("Range(0, 10, 2):", list(range(0, 10, 2)))
|
package/examples/lists_dicts.py
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# 列表操作
|
|
2
|
-
fruits = ["apple", "banana", "cherry"]
|
|
3
|
-
print("Fruits:", fruits)
|
|
4
|
-
print("First fruit:", fruits[0])
|
|
5
|
-
print("Length:", len(fruits))
|
|
6
|
-
|
|
7
|
-
# 列表方法
|
|
8
|
-
fruits.append("date")
|
|
9
|
-
print("After append:", fruits)
|
|
10
|
-
|
|
11
|
-
# 列表推导式
|
|
12
|
-
numbers = [1, 2, 3, 4, 5]
|
|
13
|
-
squared = [x * x for x in numbers]
|
|
14
|
-
print("Squared:", squared)
|
|
15
|
-
|
|
16
|
-
# 字典操作
|
|
17
|
-
person = {"name": "Alice", "age": 30, "city": "NYC"}
|
|
18
|
-
print("Person:", person)
|
|
19
|
-
print("Name:", person["name"])
|
|
20
|
-
print("Age:", person["age"])
|
|
21
|
-
|
|
22
|
-
# 字典遍历
|
|
23
|
-
print("Dictionary items:")
|
|
24
|
-
for key in person:
|
|
25
|
-
print(f" {key}: {person[key]}")
|
|
26
|
-
|
|
27
|
-
# 字典推导式
|
|
28
|
-
dict_squared = {x: x*x for x in [1, 2, 3]}
|
|
29
|
-
print("Dict squared:", dict_squared)
|
package/examples/loops.py
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# 循环示例
|
|
2
|
-
|
|
3
|
-
# for 循环
|
|
4
|
-
print("For loop:")
|
|
5
|
-
for i in range(5):
|
|
6
|
-
print(f"i = {i}")
|
|
7
|
-
|
|
8
|
-
# while 循环
|
|
9
|
-
print("\nWhile loop:")
|
|
10
|
-
count = 0
|
|
11
|
-
while count < 5:
|
|
12
|
-
print(f"count = {count}")
|
|
13
|
-
count += 1
|
|
14
|
-
|
|
15
|
-
# 列表遍历
|
|
16
|
-
print("\nList iteration:")
|
|
17
|
-
numbers = [1, 2, 3, 4, 5]
|
|
18
|
-
for num in numbers:
|
|
19
|
-
print(f"num = {num}")
|
package/examples/math_ops.py
DELETED
package/examples/nan_set.py
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# 数字类型和运算
|
|
2
|
-
# 整数
|
|
3
|
-
a = 10
|
|
4
|
-
b = 3
|
|
5
|
-
|
|
6
|
-
print("Integer operations:")
|
|
7
|
-
print(f"{a} + {b} = {a + b}")
|
|
8
|
-
print(f"{a} - {b} = {a - b}")
|
|
9
|
-
print(f"{a} * {b} = {a * b}")
|
|
10
|
-
print(f"{a} / {b} = {a / b}")
|
|
11
|
-
print(f"{a} // {b} = {a // b}")
|
|
12
|
-
print(f"{a} % {b} = {a % b}")
|
|
13
|
-
print(f"{a} ** {b} = {a ** b}")
|
|
14
|
-
|
|
15
|
-
# 浮点数
|
|
16
|
-
x = 3.14
|
|
17
|
-
y = 2.0
|
|
18
|
-
|
|
19
|
-
print("\nFloating point operations:")
|
|
20
|
-
print(f"{x} + {y} = {x + y}")
|
|
21
|
-
print(f"{x} * {y} = {x * y}")
|
|
22
|
-
|
|
23
|
-
# 复数
|
|
24
|
-
c = 3 + 4j
|
|
25
|
-
d = 1 + 2j
|
|
26
|
-
|
|
27
|
-
print("\nComplex numbers:")
|
|
28
|
-
print(f"c = {c}")
|
|
29
|
-
print(f"d = {d}")
|
|
30
|
-
print(f"c + d = {c + d}")
|
|
31
|
-
print(f"c * d = {c * d}")
|
|
32
|
-
print(f"Real part of c: {c.real}")
|
|
33
|
-
print(f"Imaginary part of c: {c.imag}")
|
|
34
|
-
|
|
35
|
-
# 数值转换
|
|
36
|
-
print("\nNumber conversions:")
|
|
37
|
-
print(f"abs(-5) = {abs(-5)}")
|
|
38
|
-
print(f"round(3.7) = {round(3.7)}")
|
|
39
|
-
print(f"round(3.14159, 2) = {round(3.14159, 2)}")
|
|
40
|
-
print(f"max(5, 10, 3) = {max(5, 10, 3)}")
|
|
41
|
-
print(f"min(5, 10, 3) = {min(5, 10, 3)}")
|
|
42
|
-
print(f"sum([1, 2, 3, 4]) = {sum([1, 2, 3, 4])}")
|
|
43
|
-
|
|
44
|
-
# 按位运算
|
|
45
|
-
print("\nBitwise operations:")
|
|
46
|
-
print(f"5 & 3 = {5 & 3}")
|
|
47
|
-
print(f"5 | 3 = {5 | 3}")
|
|
48
|
-
print(f"5 ^ 3 = {5 ^ 3}")
|
|
49
|
-
print(f"~5 = {~5}")
|
|
50
|
-
print(f"5 << 1 = {5 << 1}")
|
|
51
|
-
print(f"5 >> 1 = {5 >> 1}")
|
package/examples/sets.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# 集合操作
|
|
2
|
-
set1 = {1, 2, 3, 4, 5}
|
|
3
|
-
set2 = {4, 5, 6, 7, 8}
|
|
4
|
-
|
|
5
|
-
print("set1:", sorted(set1))
|
|
6
|
-
print("set2:", sorted(set2))
|
|
7
|
-
|
|
8
|
-
# 集合操作
|
|
9
|
-
print("Union:", sorted(set1 | set2))
|
|
10
|
-
print("Intersection:", sorted(set1 & set2))
|
|
11
|
-
print("Difference:", sorted(set1 - set2))
|
|
12
|
-
print("Symmetric difference:", sorted(set1 ^ set2))
|
|
13
|
-
|
|
14
|
-
# 集合方法
|
|
15
|
-
set3 = {1, 2, 3}
|
|
16
|
-
set3.add(4)
|
|
17
|
-
print("After add(4):", sorted(set3))
|
|
18
|
-
|
|
19
|
-
set3.update([5, 6])
|
|
20
|
-
print("After update:", sorted(set3))
|
|
21
|
-
|
|
22
|
-
set3.remove(1)
|
|
23
|
-
print("After remove(1):", sorted(set3))
|
|
24
|
-
|
|
25
|
-
# 集合成员检测
|
|
26
|
-
print("2 in set3:", 2 in set3)
|
|
27
|
-
print("10 not in set3:", 10 not in set3)
|
|
28
|
-
|
|
29
|
-
# 空集合
|
|
30
|
-
empty_set = set()
|
|
31
|
-
print("Empty set:", sorted(empty_set))
|
|
32
|
-
|
|
33
|
-
# 集合中的唯一元素
|
|
34
|
-
numbers = [1, 1, 2, 2, 3, 3, 4, 5]
|
|
35
|
-
unique = set(numbers)
|
|
36
|
-
print("Unique numbers:", sorted(unique))
|
package/examples/slicing.py
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# 切片操作
|
|
2
|
-
text = "Python Programming"
|
|
3
|
-
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
4
|
-
|
|
5
|
-
# 基本切片
|
|
6
|
-
print("text[0:6]:", text[0:6])
|
|
7
|
-
print("text[7:]:", text[7:])
|
|
8
|
-
print("text[:6]:", text[:6])
|
|
9
|
-
print("text[:]:", text[:])
|
|
10
|
-
|
|
11
|
-
# 负索引切片
|
|
12
|
-
print("text[-11:]:", text[-11:])
|
|
13
|
-
print("text[:-12]:", text[:-12])
|
|
14
|
-
|
|
15
|
-
# 步长切片
|
|
16
|
-
print("numbers[::2]:", numbers[::2])
|
|
17
|
-
print("numbers[1::2]:", numbers[1::2])
|
|
18
|
-
print("numbers[::-1]:", numbers[::-1])
|
|
19
|
-
print("numbers[5:1:-1]:", numbers[5:1:-1])
|
|
20
|
-
|
|
21
|
-
# 列表切片赋值
|
|
22
|
-
list1 = [1, 2, 3, 4, 5]
|
|
23
|
-
list1[1:3] = [20, 30]
|
|
24
|
-
print("After slice assignment:", list1)
|
|
25
|
-
|
|
26
|
-
# 列表切片删除
|
|
27
|
-
list2 = [1, 2, 3, 4, 5]
|
|
28
|
-
del list2[1:3]
|
|
29
|
-
print("After slice deletion:", list2)
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# 字符串格式化
|
|
2
|
-
name = "Alice"
|
|
3
|
-
age = 30
|
|
4
|
-
|
|
5
|
-
# 使用 % 操作符 (旧式)
|
|
6
|
-
print("Old style: %s is %d years old" % (name, age))
|
|
7
|
-
|
|
8
|
-
# 使用 .format() 方法
|
|
9
|
-
print("Format method: {} is {} years old".format(name, age))
|
|
10
|
-
print("Format indexed: {0} is {1} years old".format(name, age))
|
|
11
|
-
print("Format named: {name} is {age} years old".format(name=name, age=age))
|
|
12
|
-
|
|
13
|
-
# f-string (Python 3.6+)
|
|
14
|
-
print(f"F-string: {name} is {age} years old")
|
|
15
|
-
print(f"F-string expression: {name} is {age + 5} in 5 years")
|
|
16
|
-
|
|
17
|
-
# 数字格式化
|
|
18
|
-
pi = 3.14159
|
|
19
|
-
print(f"Pi: {pi:.2f}")
|
|
20
|
-
print(f"Pi: {pi:.4f}")
|
|
21
|
-
|
|
22
|
-
# 宽度和对齐
|
|
23
|
-
print(f"Left aligned: {name:<10} End")
|
|
24
|
-
print(f"Right aligned: {name:>10} End")
|
|
25
|
-
print(f"Center aligned: {name:^10} End")
|
|
26
|
-
|
|
27
|
-
# 数字格式
|
|
28
|
-
count = 42
|
|
29
|
-
print(f"Decimal: {count:d}")
|
|
30
|
-
print(f"Binary: {count:b}")
|
|
31
|
-
print(f"Hex: {count:x}")
|
|
32
|
-
print(f"Octal: {count:o}")
|
|
33
|
-
|
|
34
|
-
# 百分比
|
|
35
|
-
percentage = 0.75
|
|
36
|
-
print(f"Percentage: {percentage:.1%}")
|
package/examples/strings.py
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# 字符串操作
|
|
2
|
-
name = "Python"
|
|
3
|
-
print("Hello, " + name)
|
|
4
|
-
|
|
5
|
-
# 字符串索引和切片
|
|
6
|
-
text = "Programming"
|
|
7
|
-
print(f"First char: {text[0]}")
|
|
8
|
-
print(f"Last char: {text[-1]}")
|
|
9
|
-
print(f"Slice [0:7]: {text[0:7]}")
|
|
10
|
-
|
|
11
|
-
# 字符串方法
|
|
12
|
-
message = "hello world"
|
|
13
|
-
print(f"Upper: {message.upper()}")
|
|
14
|
-
print(f"Replace: {message.replace('world', 'Python')}")
|
|
15
|
-
|
|
16
|
-
# 多行字符串
|
|
17
|
-
doc = """
|
|
18
|
-
This is a
|
|
19
|
-
multi-line
|
|
20
|
-
string
|
|
21
|
-
"""
|
|
22
|
-
print(doc)
|
package/examples/tuples.py
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# 元组操作
|
|
2
|
-
# 元组是不可变的序列
|
|
3
|
-
tuple1 = (1, 2, 3, 4, 5)
|
|
4
|
-
print("tuple1:", tuple1)
|
|
5
|
-
print("Length:", len(tuple1))
|
|
6
|
-
|
|
7
|
-
# 元组索引和切片
|
|
8
|
-
print("First element:", tuple1[0])
|
|
9
|
-
print("Last element:", tuple1[-1])
|
|
10
|
-
print("Slice [1:4]:", tuple1[1:4])
|
|
11
|
-
|
|
12
|
-
# 元组拆包
|
|
13
|
-
a, b, c = (10, 20, 30)
|
|
14
|
-
print(f"Unpacked: a={a}, b={b}, c={c}")
|
|
15
|
-
|
|
16
|
-
# 多个返回值(本质是元组)
|
|
17
|
-
def get_coordinates():
|
|
18
|
-
return 10, 20, 30
|
|
19
|
-
|
|
20
|
-
x, y, z = get_coordinates()
|
|
21
|
-
print(f"Coordinates: x={x}, y={y}, z={z}")
|
|
22
|
-
|
|
23
|
-
# 元组方法
|
|
24
|
-
tuple2 = (1, 2, 3, 2, 1)
|
|
25
|
-
print("count(2):", tuple2.count(2))
|
|
26
|
-
print("index(3):", tuple2.index(3))
|
|
27
|
-
|
|
28
|
-
# 单元素元组
|
|
29
|
-
single = (42,)
|
|
30
|
-
print("Single element tuple:", single)
|
|
31
|
-
print("Type:", type(single))
|
|
32
|
-
|
|
33
|
-
# 元组连接
|
|
34
|
-
tuple3 = (1, 2) + (3, 4)
|
|
35
|
-
print("Concatenated:", tuple3)
|
|
36
|
-
|
|
37
|
-
# 元组重复
|
|
38
|
-
tuple4 = (1, 2) * 3
|
|
39
|
-
print("Repeated:", tuple4)
|
|
40
|
-
|
|
41
|
-
# 嵌套元组
|
|
42
|
-
nested = ((1, 2), (3, 4), (5, 6))
|
|
43
|
-
print("Nested:", nested)
|
|
44
|
-
print("nested[0]:", nested[0])
|
|
45
|
-
print("nested[1][1]:", nested[1][1])
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# 类型转换
|
|
2
|
-
x = "123"
|
|
3
|
-
y = 45.67
|
|
4
|
-
z = True
|
|
5
|
-
|
|
6
|
-
# 字符串到整数
|
|
7
|
-
print("int('123'):", int(x))
|
|
8
|
-
print("int(45.67):", int(y))
|
|
9
|
-
|
|
10
|
-
# 字符串到浮点数
|
|
11
|
-
print("float('123'):", float(x))
|
|
12
|
-
print("float(45):", float(45))
|
|
13
|
-
|
|
14
|
-
# 整数到字符串
|
|
15
|
-
print("str(123):", str(123))
|
|
16
|
-
print("str(45.67):", str(y))
|
|
17
|
-
|
|
18
|
-
# 布尔转换
|
|
19
|
-
print("bool(1):", bool(1))
|
|
20
|
-
print("bool(0):", bool(0))
|
|
21
|
-
print("bool(''):", bool(""))
|
|
22
|
-
print("bool('hello'):", bool("hello"))
|
|
23
|
-
print("bool([]):", bool([]))
|
|
24
|
-
print("bool([1, 2]):", bool([1, 2]))
|
|
25
|
-
|
|
26
|
-
# 列表/元组/集合之间的转换
|
|
27
|
-
list1 = [1, 2, 3, 4, 5]
|
|
28
|
-
tuple1 = tuple(list1)
|
|
29
|
-
set1 = set(list1)
|
|
30
|
-
print("List to tuple:", tuple1)
|
|
31
|
-
print("List to set:", set1)
|
|
32
|
-
|
|
33
|
-
list2 = list(set1)
|
|
34
|
-
print("Set to list:", list2)
|
|
35
|
-
|
|
36
|
-
# 类型检查
|
|
37
|
-
print("type(123):", type(123))
|
|
38
|
-
print("type('hello'):", type("hello"))
|
|
39
|
-
print("type(3.14):", type(3.14))
|
|
40
|
-
print("type([1, 2]):", type([1, 2]))
|
|
41
|
-
print("isinstance(123, int):", isinstance(123, int))
|
package/jest.config.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
testEnvironment: 'node',
|
|
4
|
-
roots: ['<rootDir>/tests'],
|
|
5
|
-
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
|
|
6
|
-
transform: {
|
|
7
|
-
'^.+\\.ts$': 'ts-jest',
|
|
8
|
-
},
|
|
9
|
-
collectCoverageFrom: [
|
|
10
|
-
'src/**/*.ts',
|
|
11
|
-
'!src/**/*.d.ts',
|
|
12
|
-
],
|
|
13
|
-
coverageDirectory: 'coverage',
|
|
14
|
-
verbose: true,
|
|
15
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# compiler-runtime: Negative repeat handling
|
|
2
|
-
|
|
3
|
-
- Date: 2025-09-16
|
|
4
|
-
- Stage: test
|
|
5
|
-
- Status: resolved
|
|
6
|
-
|
|
7
|
-
## Problem / Symptom
|
|
8
|
-
|
|
9
|
-
RangeError: Invalid count value: -1 during binary * on strings/lists.
|
|
10
|
-
|
|
11
|
-
## Impact / Risk
|
|
12
|
-
|
|
13
|
-
Example execution fails when Python code repeats strings or lists with a negative count.
|
|
14
|
-
|
|
15
|
-
## Current Understanding
|
|
16
|
-
|
|
17
|
-
JavaScript String.repeat/Array(length) throw for negative counts, but Python returns empty sequences.
|
|
18
|
-
|
|
19
|
-
## Next Steps
|
|
20
|
-
|
|
21
|
-
Keep repeat behavior aligned with Python for other edge cases (e.g., non-integer counts).
|
|
22
|
-
|
|
23
|
-
## Evidence (optional)
|
|
24
|
-
|
|
25
|
-
- Tests: `scripts/verify.sh`
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# compiler-runtime: Starred assignment unpacking
|
|
2
|
-
|
|
3
|
-
- Date: 2026-01-16
|
|
4
|
-
- Stage: test
|
|
5
|
-
- Status: resolved
|
|
6
|
-
|
|
7
|
-
## Problem / Symptom
|
|
8
|
-
|
|
9
|
-
Tests failed parsing assignment targets with starred unpacking (e.g., `a, *middle, b = values`) and raised "Unexpected token in expression: *".
|
|
10
|
-
|
|
11
|
-
## Impact / Risk
|
|
12
|
-
|
|
13
|
-
Example files using starred unpacking could not be parsed or executed, blocking `scripts/verify.sh`.
|
|
14
|
-
|
|
15
|
-
## Current Understanding
|
|
16
|
-
|
|
17
|
-
Parser did not allow `*` in assignment targets and VM assignment logic only handled fixed-length tuple/list destructuring. Added a STARRED AST node for targets, parsed it in assignment targets, and implemented unpacking logic in the VM with iterable normalization.
|
|
18
|
-
|
|
19
|
-
## Next Steps
|
|
20
|
-
|
|
21
|
-
None. Re-run verify if adding more starred assignment cases.
|
|
22
|
-
|
|
23
|
-
## Evidence (optional)
|
|
24
|
-
- Tests: `scripts/verify.sh`
|