Adds strlen as a builtin function
This commit is contained in:
parent
1be24ef771
commit
a282292604
@ -475,6 +475,9 @@ def emitheader():
|
|||||||
emitln(" sys.stderr.flush()")
|
emitln(" sys.stderr.flush()")
|
||||||
emitln(" exit(1)")
|
emitln(" exit(1)")
|
||||||
emitln("")
|
emitln("")
|
||||||
|
emitln("def __strlen(a: str) -> str:")
|
||||||
|
emitln(" return str(len(a))")
|
||||||
|
emitln("")
|
||||||
emitln("# ### END OF RUNTIME ### #")
|
emitln("# ### END OF RUNTIME ### #")
|
||||||
emitln("")
|
emitln("")
|
||||||
|
|
||||||
@ -518,6 +521,8 @@ def main():
|
|||||||
mapsetkey("REGISTERID", "stdincolno", "__stdincolno")
|
mapsetkey("REGISTERID", "stdincolno", "__stdincolno")
|
||||||
mapsetkey("FUNCREG", "stdinlineno", "1")
|
mapsetkey("FUNCREG", "stdinlineno", "1")
|
||||||
mapsetkey("REGISTERID", "stdinlineno", "__stdinlineno")
|
mapsetkey("REGISTERID", "stdinlineno", "__stdinlineno")
|
||||||
|
mapsetkey("FUNCREG", "strlen", "1")
|
||||||
|
mapsetkey("REGISTERID", "strlen", "__strlen")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if eof == peek():
|
if eof == peek():
|
||||||
|
|||||||
@ -534,6 +534,9 @@ emitheader:
|
|||||||
emitln " sys.stderr.flush()"
|
emitln " sys.stderr.flush()"
|
||||||
emitln " exit(1)"
|
emitln " exit(1)"
|
||||||
emitln ""
|
emitln ""
|
||||||
|
emitln "def __strlen(a: str) -> str:"
|
||||||
|
emitln " return str(len(a))"
|
||||||
|
emitln ""
|
||||||
emitln "# ### END OF RUNTIME ### #"
|
emitln "# ### END OF RUNTIME ### #"
|
||||||
emitln ""
|
emitln ""
|
||||||
/
|
/
|
||||||
@ -579,6 +582,8 @@ main:
|
|||||||
mapsetkey "REGISTERID" "stdincolno" "__stdincolno"
|
mapsetkey "REGISTERID" "stdincolno" "__stdincolno"
|
||||||
mapsetkey "FUNCREG" "stdinlineno" "1"
|
mapsetkey "FUNCREG" "stdinlineno" "1"
|
||||||
mapsetkey "REGISTERID" "stdinlineno" "__stdinlineno"
|
mapsetkey "REGISTERID" "stdinlineno" "__stdinlineno"
|
||||||
|
mapsetkey "FUNCREG" "strlen" "1"
|
||||||
|
mapsetkey "REGISTERID" "strlen" "__strlen"
|
||||||
|
|
||||||
|
|
||||||
forever
|
forever
|
||||||
|
|||||||
@ -798,6 +798,18 @@ emitheader:
|
|||||||
emitln ";"
|
emitln ";"
|
||||||
emitln "}"
|
emitln "}"
|
||||||
emitln ""
|
emitln ""
|
||||||
|
emitln "char * __strlen(char * inp)"
|
||||||
|
emitln "{"
|
||||||
|
emitln " char * buffer = malloc(20);"
|
||||||
|
emitln " size_t len = strlen(inp);"
|
||||||
|
emit " snprintf(buffer, 20, "
|
||||||
|
emit quote
|
||||||
|
emit "%zu"
|
||||||
|
emit quote
|
||||||
|
emitln ", len);"
|
||||||
|
emitln " return buffer;"
|
||||||
|
emitln "}"
|
||||||
|
emitln ""
|
||||||
emitln "// ### END OF RUNTIME ### //"
|
emitln "// ### END OF RUNTIME ### //"
|
||||||
emitln ""
|
emitln ""
|
||||||
return
|
return
|
||||||
@ -852,6 +864,8 @@ main:
|
|||||||
mapsetkey "REGISTERID" "stdincolno" "__stdincolno"
|
mapsetkey "REGISTERID" "stdincolno" "__stdincolno"
|
||||||
mapsetkey "FUNCREG" "stdinlineno" "1"
|
mapsetkey "FUNCREG" "stdinlineno" "1"
|
||||||
mapsetkey "REGISTERID" "stdinlineno" "__stdinlineno"
|
mapsetkey "REGISTERID" "stdinlineno" "__stdinlineno"
|
||||||
|
mapsetkey "FUNCREG" "strlen" "1"
|
||||||
|
mapsetkey "REGISTERID" "strlen" "__strlen"
|
||||||
|
|
||||||
forever
|
forever
|
||||||
calc char peek
|
calc char peek
|
||||||
|
|||||||
@ -194,6 +194,10 @@ Returns the column number for stdin (starting at 1)
|
|||||||
|
|
||||||
Returns the line number for stdin (starting at 1)
|
Returns the line number for stdin (starting at 1)
|
||||||
|
|
||||||
|
#### strlen a
|
||||||
|
|
||||||
|
Returns a string with the length of a.
|
||||||
|
|
||||||
### Typing
|
### Typing
|
||||||
|
|
||||||
Every variable is of type string. Every function gets a number of strings as output, and returns another string.
|
Every variable is of type string. Every function gets a number of strings as output, and returns another string.
|
||||||
|
|||||||
@ -1,14 +1,3 @@
|
|||||||
func tst a b:
|
|
||||||
declare var
|
|
||||||
calc var add a b
|
|
||||||
emit a
|
|
||||||
emit " + "
|
|
||||||
emit b
|
|
||||||
emit " = "
|
|
||||||
emit var
|
|
||||||
emit eol
|
|
||||||
/
|
|
||||||
|
|
||||||
main:
|
main:
|
||||||
declare var
|
declare var
|
||||||
|
|
||||||
|
|||||||
4
tests/test_stdlib_functions/test_strlen.exp.stdout
Normal file
4
tests/test_stdlib_functions/test_strlen.exp.stdout
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
strlen()=0
|
||||||
|
strlen()=1
|
||||||
|
strlen()=1
|
||||||
|
strlen()=8
|
||||||
23
tests/test_stdlib_functions/test_strlen.lang0
Normal file
23
tests/test_stdlib_functions/test_strlen.lang0
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
main:
|
||||||
|
declare var
|
||||||
|
|
||||||
|
calc var strlen ""
|
||||||
|
emit "strlen()="
|
||||||
|
emit var
|
||||||
|
emit eol
|
||||||
|
|
||||||
|
calc var strlen "0"
|
||||||
|
emit "strlen()="
|
||||||
|
emit var
|
||||||
|
emit eol
|
||||||
|
|
||||||
|
calc var strlen "1"
|
||||||
|
emit "strlen()="
|
||||||
|
emit var
|
||||||
|
emit eol
|
||||||
|
|
||||||
|
calc var strlen "abcdefgh"
|
||||||
|
emit "strlen()="
|
||||||
|
emit var
|
||||||
|
emit eol
|
||||||
|
/
|
||||||
Loading…
x
Reference in New Issue
Block a user