lizard-lang 0.1.0__py3-none-any.whl

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.
lizard/__init__.py ADDED
File without changes
lizard/cli.py ADDED
@@ -0,0 +1,27 @@
1
+ import sys
2
+ from pathlib import Path
3
+
4
+ from lizard.errors import LizardSyntaxError
5
+ from lizard.transpiler import transpile
6
+ from lizard.runner import run
7
+
8
+
9
+ def main():
10
+ if len(sys.argv) != 2:
11
+ print("Usage: lizard <file.lz>")
12
+ return 1
13
+
14
+ path = sys.argv[1]
15
+
16
+ try:
17
+ source = Path(path).read_text()
18
+ python_code = transpile(source)
19
+ run(python_code)
20
+ except FileNotFoundError:
21
+ print(f"File not found: {path}")
22
+ return 1
23
+ except LizardSyntaxError as error:
24
+ print(f"Syntax error: {error}")
25
+ return 1
26
+
27
+ return 0
lizard/errors.py ADDED
@@ -0,0 +1,2 @@
1
+ class LizardSyntaxError(Exception):
2
+ pass
lizard/lexer.py ADDED
@@ -0,0 +1,69 @@
1
+ from lizard.tokens import Token, TokenType
2
+
3
+
4
+ def lex_line(line: str, line_number: int) -> list[Token]:
5
+ tokens = []
6
+ buffer = []
7
+ buffer_start_col = 1
8
+
9
+ in_string = False
10
+ quote_char = ""
11
+
12
+ i = 0
13
+
14
+ while i < len(line):
15
+ ch = line[i]
16
+ col = i + 1
17
+
18
+ if in_string:
19
+ buffer.append(ch)
20
+ if ch == quote_char:
21
+ tokens.append(Token(TokenType.STRING, "".join(buffer), line_number, buffer_start_col))
22
+ buffer = []
23
+ in_string = False
24
+ quote_char = ""
25
+ i += 1
26
+ continue
27
+
28
+ if ch in ('"', "'"):
29
+ if buffer:
30
+ tokens.append(Token(TokenType.CODE, "".join(buffer), line_number, buffer_start_col))
31
+ buffer = []
32
+ in_string = True
33
+ quote_char = ch
34
+ buffer_start_col = col
35
+ buffer.append(ch)
36
+ i += 1
37
+ continue
38
+
39
+ if ch == "#":
40
+ if buffer:
41
+ tokens.append(Token(TokenType.CODE, "".join(buffer), line_number, buffer_start_col))
42
+ buffer = []
43
+ tokens.append(Token(TokenType.COMMENT, line[i:], line_number, col))
44
+ break
45
+
46
+ if ch in (TokenType.LBRACE.value, TokenType.RBRACE.value, TokenType.SEMICOLON.value):
47
+ if buffer:
48
+ tokens.append(Token(TokenType.CODE, "".join(buffer), line_number, buffer_start_col))
49
+ buffer = []
50
+ if ch == TokenType.LBRACE.value:
51
+ tokens.append(Token(TokenType.LBRACE, ch, line_number, col))
52
+ elif ch == TokenType.RBRACE.value:
53
+ tokens.append(Token(TokenType.RBRACE, ch, line_number, col))
54
+ else:
55
+ tokens.append(Token(TokenType.SEMICOLON, ch, line_number, col))
56
+ i += 1
57
+ continue
58
+
59
+ if not buffer:
60
+ buffer_start_col = col
61
+ buffer.append(ch)
62
+ i += 1
63
+
64
+ if in_string:
65
+ tokens.append(Token(TokenType.STRING, "".join(buffer), line_number, buffer_start_col))
66
+ elif buffer:
67
+ tokens.append(Token(TokenType.CODE, "".join(buffer), line_number, buffer_start_col))
68
+
69
+ return tokens
lizard/runner.py ADDED
@@ -0,0 +1,12 @@
1
+ import ast
2
+
3
+ def run(python_code: str):
4
+ tree = ast.parse(python_code)
5
+
6
+ compiled = compile(
7
+ tree,
8
+ filename="<lizard>",
9
+ mode="exec"
10
+ )
11
+
12
+ exec(compiled)
lizard/tokens.py ADDED
@@ -0,0 +1,18 @@
1
+ from enum import Enum
2
+
3
+
4
+ class TokenType(Enum):
5
+ CODE = "CODE"
6
+ STRING = "STRING"
7
+ COMMENT = "COMMENT"
8
+ LBRACE = "{"
9
+ RBRACE = "}"
10
+ SEMICOLON = ";"
11
+
12
+
13
+ class Token:
14
+ def __init__(self, token_type, value, line, col):
15
+ self.type = token_type
16
+ self.value = value
17
+ self.line = line
18
+ self.col = col
lizard/transpiler.py ADDED
@@ -0,0 +1,76 @@
1
+ from lizard.errors import LizardSyntaxError
2
+ from lizard.lexer import lex_line
3
+ from lizard.tokens import TokenType
4
+
5
+
6
+ INDENT = " "
7
+
8
+
9
+ def transpile_line(line: str, line_number: int) -> tuple[str, bool, bool]:
10
+ result = []
11
+
12
+ opens_block = False
13
+ closes_block = False
14
+ has_comment = False
15
+
16
+ tokens = lex_line(line, line_number)
17
+
18
+ for token in tokens:
19
+ if token.type == TokenType.SEMICOLON:
20
+ continue
21
+
22
+ if token.type == TokenType.COMMENT:
23
+ result.append(token.value)
24
+ has_comment = True
25
+ break
26
+
27
+ if token.type == TokenType.LBRACE:
28
+ result.append(":")
29
+ opens_block = True
30
+ continue
31
+
32
+ if token.type == TokenType.RBRACE:
33
+ closes_block = True
34
+ continue
35
+
36
+ result.append(token.value)
37
+
38
+ transformed = "".join(result)
39
+ if not has_comment:
40
+ transformed = transformed.rstrip()
41
+
42
+ return transformed, opens_block, closes_block
43
+
44
+
45
+ def transpile(source: str) -> str:
46
+ output = []
47
+
48
+ indent_level = 0
49
+
50
+ for line_number, raw_line in enumerate(source.splitlines(), start=1):
51
+
52
+ stripped = raw_line.strip()
53
+
54
+ if not stripped:
55
+ output.append("")
56
+ continue
57
+
58
+ transformed, opens_block, closes_block = transpile_line(stripped, line_number)
59
+
60
+ if closes_block:
61
+ indent_level -= 1
62
+
63
+ if indent_level < 0:
64
+ raise LizardSyntaxError(f"Unexpected '}}' at line {line_number}")
65
+
66
+ output.append(
67
+ INDENT * indent_level + transformed
68
+ )
69
+
70
+ if opens_block:
71
+ indent_level += 1
72
+
73
+ if indent_level != 0:
74
+ raise LizardSyntaxError("Unclosed block")
75
+
76
+ return "\n".join(output)
@@ -0,0 +1,4 @@
1
+ Metadata-Version: 2.4
2
+ Name: lizard-lang
3
+ Version: 0.1.0
4
+ Requires-Python: >=3.10
@@ -0,0 +1,12 @@
1
+ lizard/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ lizard/cli.py,sha256=FTCrZPILYoGlRdEz25EfIjr_cSsTbKARtd8averO3zE,586
3
+ lizard/errors.py,sha256=t0S_SV1AUVUUq-1WGoOTi5nIPPeNsDtX-O-93cWky-M,44
4
+ lizard/lexer.py,sha256=Z5fcVshb5t1_NuNKvAwwbI5-sQAH4SuRcJphBQlvuoI,2195
5
+ lizard/runner.py,sha256=HS1EL9lopIERet8_AurUuYinS-rC-3U552X9KYzSxHs,186
6
+ lizard/tokens.py,sha256=HdqJ8g8h1mCeOXB8xTTAZwwc64hBnX9vk-O1VWVpSOo,339
7
+ lizard/transpiler.py,sha256=Q44vId7uAxlNdftgaeKHRUubaYiX-8X9xxIYZTVoRqQ,1747
8
+ lizard_lang-0.1.0.dist-info/METADATA,sha256=YyDAZKlNMCyq7weDt-KG8VsCu0bOWZ5E9uSQO4VuW5A,79
9
+ lizard_lang-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
10
+ lizard_lang-0.1.0.dist-info/entry_points.txt,sha256=vLC-ZQSanD6FbvVrO7k8KSMqTM7vaXZzk5iInH4sfCk,43
11
+ lizard_lang-0.1.0.dist-info/top_level.txt,sha256=htzoeiYeZQoGDPT6rOaNgWCvXodj5ZyK6Pcg9LN51I8,7
12
+ lizard_lang-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ lizard = lizard.cli:main
@@ -0,0 +1 @@
1
+ lizard