From 5369c15e05aa5ada1bab159b62ca69ed0e170e2c Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sun, 25 Jan 2026 13:02:08 +0100 Subject: [PATCH] Adds intinc as a buildint function --- 0-lang0py/lang0py.py | 5 ++++ 1-lang0py/lang0py.lang0 | 5 ++++ 2-lang0c/lang0c.lang0 | 15 ++++++++++ README.md | 6 ++++ .../test_intinc.exp.stdout | 5 ++++ tests/test_stdlib_functions/test_intinc.lang0 | 28 +++++++++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 tests/test_stdlib_functions/test_intinc.exp.stdout create mode 100644 tests/test_stdlib_functions/test_intinc.lang0 diff --git a/0-lang0py/lang0py.py b/0-lang0py/lang0py.py index cea3e4c..97cb13b 100644 --- a/0-lang0py/lang0py.py +++ b/0-lang0py/lang0py.py @@ -478,6 +478,9 @@ def emitheader(): emitln("def __strlen(a: str) -> str:") emitln(" return str(len(a))") emitln("") + emitln("def __intinc(a: str) -> str:") + emitln(" return str(int(a) + 1)") + emitln("") emitln("# ### END OF RUNTIME ### #") emitln("") @@ -503,6 +506,8 @@ def main(): mapsetkey("REGISTERID", "emit", "__emit") mapsetkey("FUNCREG", "eq", "1") mapsetkey("REGISTERID", "eq", "__eq") + mapsetkey("FUNCREG", "intinc", "1") + mapsetkey("REGISTERID", "intinc", "__intinc") mapsetkey("FUNCREG", "lt", "1") mapsetkey("REGISTERID", "lt", "__lt") mapsetkey("FUNCREG", "not", "1") diff --git a/1-lang0py/lang0py.lang0 b/1-lang0py/lang0py.lang0 index abea5db..3435074 100644 --- a/1-lang0py/lang0py.lang0 +++ b/1-lang0py/lang0py.lang0 @@ -537,6 +537,9 @@ emitheader: emitln "def __strlen(a: str) -> str:" emitln " return str(len(a))" emitln "" + emitln "def __intinc(a: str) -> str:" + emitln " return str(int(a) + 1)" + emitln "" emitln "# ### END OF RUNTIME ### #" emitln "" / @@ -564,6 +567,8 @@ main: mapsetkey "REGISTERID" "emit" "__emit" mapsetkey "FUNCREG" "eq" "1" mapsetkey "REGISTERID" "eq" "__eq" + mapsetkey "FUNCREG" "intinc" "1" + mapsetkey "REGISTERID" "intinc" "__intinc" mapsetkey "FUNCREG" "lt" "1" mapsetkey "REGISTERID" "lt" "__lt" mapsetkey "FUNCREG" "not" "1" diff --git a/2-lang0c/lang0c.lang0 b/2-lang0c/lang0c.lang0 index a297535..39082bb 100644 --- a/2-lang0c/lang0c.lang0 +++ b/2-lang0c/lang0c.lang0 @@ -810,6 +810,19 @@ emitheader: emitln " return buffer;" emitln "}" emitln "" + emitln "char * __intinc(char * inp)" + emitln "{" + emitln " char * buffer = malloc(20);" + emitln " char * endptr;" + emitln " unsigned long long number = strtoull(inp, &endptr, 10);" + emit " snprintf(buffer, 20, " + emit quote + emit "%llu" + emit quote + emitln ", number + 1);" + emitln " return buffer;" + emitln "}" + emitln "" emitln "// ### END OF RUNTIME ### //" emitln "" return @@ -846,6 +859,8 @@ main: mapsetkey "REGISTERID" "emit" "__emit" mapsetkey "FUNCREG" "eq" "1" mapsetkey "REGISTERID" "eq" "__eq" + mapsetkey "FUNCREG" "intinc" "1" + mapsetkey "REGISTERID" "intinc" "__intinc" mapsetkey "FUNCREG" "lt" "1" mapsetkey "REGISTERID" "lt" "__lt" mapsetkey "FUNCREG" "not" "1" diff --git a/README.md b/README.md index 7d66304..5098cf5 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,12 @@ Return true if the given strings are the same. Return true if a would sort before b. +#### intinc + +Assumes `a` is a string containing solely a decimal integer. + +Returns a increased by one. + #### mapclear mapname Maps are global and can be used from any function. diff --git a/tests/test_stdlib_functions/test_intinc.exp.stdout b/tests/test_stdlib_functions/test_intinc.exp.stdout new file mode 100644 index 0000000..1cf0f2d --- /dev/null +++ b/tests/test_stdlib_functions/test_intinc.exp.stdout @@ -0,0 +1,5 @@ +0 + 1: 1 +5 + 1: 6 +9 + 1: 10 +15 + 1: 16 +99 + 1: 100 diff --git a/tests/test_stdlib_functions/test_intinc.lang0 b/tests/test_stdlib_functions/test_intinc.lang0 new file mode 100644 index 0000000..055de33 --- /dev/null +++ b/tests/test_stdlib_functions/test_intinc.lang0 @@ -0,0 +1,28 @@ +main: + declare result + + emit "0 + 1: " + calc result intinc "0" + emit result + emit eol + + emit "5 + 1: " + calc result intinc "5" + emit result + emit eol + + emit "9 + 1: " + calc result intinc "9" + emit result + emit eol + + emit "15 + 1: " + calc result intinc "15" + emit result + emit eol + + emit "99 + 1: " + calc result intinc "99" + emit result + emit eol +/