🐐 Several fixes
This commit is contained in:
@@ -92,7 +92,7 @@ def num_to_int(n, m, s=False):
|
||||
|
||||
return sum(nums)
|
||||
|
||||
def int_to_num(n, m, s=False):
|
||||
def int_to_num(n, m, s=False) -> str:
|
||||
if n < 0:
|
||||
if not s:
|
||||
raise CentvrionError("Cannot display negative numbers without 'SVBNVLLA' module")
|
||||
@@ -120,7 +120,7 @@ def int_to_num(n, m, s=False):
|
||||
|
||||
return ''.join(nums)
|
||||
|
||||
def make_string(val, magnvm=False, svbnvlla=False):
|
||||
def make_string(val, magnvm=False, svbnvlla=False) -> str:
|
||||
if isinstance(val, ValStr):
|
||||
return val.value()
|
||||
elif isinstance(val, ValInt):
|
||||
@@ -128,7 +128,7 @@ def make_string(val, magnvm=False, svbnvlla=False):
|
||||
elif isinstance(val, ValFrac):
|
||||
return fraction_to_frac(val.value(), magnvm, svbnvlla)
|
||||
elif isinstance(val, ValBool):
|
||||
return "VERVS" if val.value() else "FALSVS"
|
||||
return "VERITAS" if val.value() else "FALSITAS"
|
||||
elif isinstance(val, ValNul):
|
||||
return "NVLLVS"
|
||||
elif isinstance(val, ValList):
|
||||
@@ -166,7 +166,7 @@ def frac_to_fraction(s, magnvm=False, svbnvlla=False):
|
||||
|
||||
return total
|
||||
|
||||
def fraction_to_frac(f, magnvm=False, svbnvlla=False):
|
||||
def fraction_to_frac(f, magnvm=False, svbnvlla=False) -> str:
|
||||
if f < 0:
|
||||
if not svbnvlla:
|
||||
raise CentvrionError("Cannot display negative numbers without 'SVBNVLLA' module")
|
||||
@@ -200,16 +200,20 @@ class Node(BaseBox):
|
||||
def _eval(self, vtable):
|
||||
raise NotImplementedError
|
||||
|
||||
def print(self):
|
||||
return "Node()"
|
||||
|
||||
|
||||
class ExpressionStatement(Node):
|
||||
def __init__(self, expression) -> None:
|
||||
def __init__(self, expression: Node) -> None:
|
||||
self.expression = expression
|
||||
|
||||
def __eq__(self, other):
|
||||
return type(self) == type(other) and self.expression == other.expression
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.expression.__repr__()
|
||||
expr = repr(self.expression).replace('\n', '\n ')
|
||||
return f"ExpressionStatement(\n {expr}\n)"
|
||||
|
||||
def print(self):
|
||||
return self.expression.print()
|
||||
@@ -284,7 +288,7 @@ class String(Node):
|
||||
|
||||
|
||||
class Numeral(Node):
|
||||
def __init__(self, value) -> None:
|
||||
def __init__(self, value: str) -> None:
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -301,7 +305,7 @@ class Numeral(Node):
|
||||
|
||||
|
||||
class Fractio(Node):
|
||||
def __init__(self, value) -> None:
|
||||
def __init__(self, value: str) -> None:
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -372,7 +376,7 @@ class ID(Node):
|
||||
|
||||
|
||||
class Designa(Node):
|
||||
def __init__(self, variable: ID, value) -> None:
|
||||
def __init__(self, variable: ID, value: Node) -> None:
|
||||
self.id = variable
|
||||
self.value = value
|
||||
|
||||
@@ -426,7 +430,7 @@ class DesignaIndex(Node):
|
||||
|
||||
|
||||
class Defini(Node):
|
||||
def __init__(self, name, parameters, statements) -> None:
|
||||
def __init__(self, name: ID, parameters: list[ID], statements: list[Node]) -> None:
|
||||
self.name = name
|
||||
self.parameters = parameters
|
||||
self.statements = statements
|
||||
@@ -453,7 +457,7 @@ class Defini(Node):
|
||||
|
||||
|
||||
class Redi(Node):
|
||||
def __init__(self, values) -> None:
|
||||
def __init__(self, values: list[Node]) -> None:
|
||||
self.values = values
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -524,7 +528,7 @@ class Nullus(Node):
|
||||
|
||||
|
||||
class BinOp(Node):
|
||||
def __init__(self, left, right, op) -> None:
|
||||
def __init__(self, left: Node, right: Node, op: str) -> None:
|
||||
self.left = left
|
||||
self.right = right
|
||||
self.op = op
|
||||
@@ -601,7 +605,7 @@ class BinOp(Node):
|
||||
|
||||
|
||||
class UnaryMinus(Node):
|
||||
def __init__(self, expr):
|
||||
def __init__(self, expr: Node):
|
||||
self.expr = expr
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -617,9 +621,12 @@ class UnaryMinus(Node):
|
||||
if "SVBNVLLA" not in vtable["#modules"]:
|
||||
raise CentvrionError("Cannot use unary minus without 'SVBNVLLA' module")
|
||||
vtable, val = self.expr.eval(vtable)
|
||||
if not isinstance(val, ValInt):
|
||||
if isinstance(val, ValInt):
|
||||
return vtable, ValInt(-val.value())
|
||||
elif isinstance(val, ValFrac):
|
||||
return vtable, ValFrac(-val.value())
|
||||
else:
|
||||
raise CentvrionError("Unary minus requires a number")
|
||||
return vtable, ValInt(-val.value())
|
||||
|
||||
|
||||
class UnaryNot(Node):
|
||||
@@ -661,9 +668,12 @@ class ArrayIndex(Node):
|
||||
vtable, index = self.index.eval(vtable)
|
||||
if not isinstance(array, ValList):
|
||||
raise CentvrionError("Cannot index a non-array value")
|
||||
if not isinstance(index, ValInt):
|
||||
if isinstance(index, ValInt):
|
||||
i = index.value()
|
||||
elif isinstance(index, ValFrac) and index.value().denominator == 1:
|
||||
i = index.value().numerator
|
||||
else:
|
||||
raise CentvrionError("Array index must be a number")
|
||||
i = index.value()
|
||||
lst = array.value()
|
||||
if i < 1 or i > len(lst):
|
||||
raise CentvrionError(f"Index {i} out of range for array of length {len(lst)}")
|
||||
@@ -682,7 +692,10 @@ class SiStatement(Node):
|
||||
def __repr__(self) -> str:
|
||||
test = repr(self.test)
|
||||
statements = f"statements([{rep_join(self.statements)}])"
|
||||
else_part = f"statements([{rep_join(self.else_part) if self.else_part else ''}])"
|
||||
if self.else_part is None:
|
||||
else_part = "None"
|
||||
else:
|
||||
else_part = f"statements([{rep_join(self.else_part)}])"
|
||||
si_string = rep_join([test, statements, else_part])
|
||||
return f"Si({si_string})"
|
||||
|
||||
@@ -713,7 +726,7 @@ class SiStatement(Node):
|
||||
|
||||
|
||||
class DumStatement(Node):
|
||||
def __init__(self, test, statements) -> None:
|
||||
def __init__(self, test: Node, statements: list[Node]) -> None:
|
||||
self.test = test
|
||||
self.statements = statements
|
||||
|
||||
@@ -838,7 +851,7 @@ class Invoca(Node):
|
||||
|
||||
|
||||
class BuiltIn(Node):
|
||||
def __init__(self, builtin, parameters) -> None:
|
||||
def __init__(self, builtin: str, parameters: list[Node]) -> None:
|
||||
self.builtin = builtin
|
||||
self.parameters = parameters
|
||||
|
||||
@@ -907,7 +920,7 @@ class BuiltIn(Node):
|
||||
|
||||
|
||||
class Program(BaseBox):
|
||||
def __init__(self, module_calls: list[ModuleCall], statements) -> None:
|
||||
def __init__(self, module_calls: list[ModuleCall], statements: list[Node]) -> None:
|
||||
self.modules = module_calls
|
||||
self.statements = statements
|
||||
|
||||
|
||||
@@ -221,11 +221,11 @@ static int write_val(CentValue v, char *buf, int bufsz) {
|
||||
|
||||
case CENT_BOOL:
|
||||
if (v.bval) {
|
||||
if (buf && bufsz > 5) { memcpy(buf, "VERVS", 5); buf[5] = '\0'; }
|
||||
return 5;
|
||||
if (buf && bufsz > 7) { memcpy(buf, "VERITAS", 7); buf[7] = '\0'; }
|
||||
return 7;
|
||||
} else {
|
||||
if (buf && bufsz > 6) { memcpy(buf, "FALSVS", 6); buf[6] = '\0'; }
|
||||
return 6;
|
||||
if (buf && bufsz > 8) { memcpy(buf, "FALSITAS", 8); buf[8] = '\0'; }
|
||||
return 8;
|
||||
}
|
||||
|
||||
case CENT_NULL:
|
||||
@@ -527,9 +527,13 @@ void cent_list_push(CentValue *lst, CentValue v) {
|
||||
CentValue cent_list_index(CentValue lst, CentValue idx) {
|
||||
if (lst.type != CENT_LIST)
|
||||
cent_type_error("index requires a list");
|
||||
if (idx.type != CENT_INT)
|
||||
long i;
|
||||
if (idx.type == CENT_INT)
|
||||
i = idx.ival;
|
||||
else if (idx.type == CENT_FRAC && idx.fval.den == 1)
|
||||
i = idx.fval.num;
|
||||
else
|
||||
cent_type_error("list index must be an integer");
|
||||
long i = idx.ival;
|
||||
if (i < 1 || i > lst.lval.len)
|
||||
cent_runtime_error("list index out of range");
|
||||
return lst.lval.items[i - 1];
|
||||
|
||||
Reference in New Issue
Block a user