Skip to content
Werner Stoop edited this page Dec 26, 2013 · 5 revisions

Musl, the Marginally Useful Scripting Language or My UnStructured Language

An interpreter for a silly unstructured programming language.

License

Author: Werner Stoop.

These sources are provided under the terms of the unlicense:

 This is free and unencumbered software released into the public domain.
 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.
 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 For more information, please refer to <http://unlicense.org/>

Syntax

The following describes the syntax of the interpreter. Consult the examples if it is unclear.

 program ::= [line]*
 line ::= [label ':'] stmts <LF>
            | NUMBER stmts <LF>
 stmts ::= stmt [':' [<LF>+] stmts]
 stmt ::= [LET] ident ['[' expr ']'] '=' expr
        | ident '(' fparams ')'
        | GOTO label
        | GOSUB label
        | ON expr GOTO label [',' label]*
        | ON expr GOSUB label [',' label]*
        | RETURN
        | IF expr THEN [<LF>+] stmts
        | FOR ident = expr TO expr [STEP expr] DO [<LF>+] stmts [<LF>+] NEXT
        | END
 fparams ::= '(' [expr ',' expr ',' ...] ')'
 expr ::= and_expr [OR and_expr]*
 and_expr ::= not_expr [AND not_expr]*
 not_expr ::= [NOT] comp_expr
 comp_expr ::= cat_expr [('='|'<'|'>'|'~') cat_expr]
 cat_expr ::= add_expr ['&' add_expr]*
 add_expr ::= mul_expr [('+'|'-') mul_expr]*
 mul_expr ::= uexpr [('*'|'/'|'%') uexpr]*
 uexpr ::= ['-'|'+'] atom
 atom ::= '(' expr ')'
        |  ident
        |  ident '[' expr ']'
        |  ident '(' [fparams] ')'
        |  number
        |  string
        |  '@' ident

Functions

These functions are available to Musl scripts.

Built-In Functions

The Following built-in functions are available to all scripts.

VAL(x$)

Converts the string x$ to a number.

STR$(x)

Converts the number x to a string.

LEN(x$)

Returns the length of string x$

LEFT$(s$, n)

Returns the n leftmost characters in s$

RIGHT$(s$, n)

Returns the n rightmost characters in s$

MID$(s$, n, m)

Returns the all the characters in s$ between n and m inclusive.

The string is indexed from 1, that is MID$("Hello World From Musl", 7, 11) will return "World"

UCASE$(x$)

Converts the string x$ to uppercase.

LCASE$(x$)

Converts the string x$ to lowercase.

TRIM$(x$)

Removes leading and trailing whitespace from string x$.

INSTR(str$, find$)

Searches for find$ in str$ and returns the index.

It returns 0 if find$ was not found.

IFF(cond, then_val, else_val)

If the condition cond is true, it returns then_val, otherwise it returns else_val

DATA(@list$, item1, item2, item3, ...)

Populates an array named list$.

Note: The first parameter is a string containing the name of the array. A call

 DATA(@array$, "Alice", "Bob", "Carol")

is equivalent to the statements:

 LET array$[1] = "Alice"
 LET array$[2] = "Bob"
 LET array$[3] = "Carol"

list$["length"] will contain the number of items in the array.

Subsequent calls to DATA() on the same array will append more data.

It returns the number of items inserted into the array.

MAP(@mymap, key1, val1, key2, val2, ...)

Initializes mymap as an array of key-value pairs.

A call

 MAP(@mymap, "Alice", 111, "Bob", 222, "Carol", 333)

is equivalent to the statements:

 LET mymap["Alice"] = 111
 LET mymap["Bob"] = 222
 LET mymap["Carol"] = 333

PUSH(val)

Pushes a value val onto an internal stack where it can be popped later through the POP() function.

It is used to simulate local variables in subroutines.

Example:

 PUSH(foo)

Note: Don't access __stack[] and __sp directly.

POP()

Pops a value from the stack that was pushed earlier through the PUSH() function.

Example:

 foo = POP()

THROW(msg$)

Throws an error with the specified message.