https://github.com/pkopel/pig
Simple interpreted imperative language
https://github.com/pkopel/pig
imperative-programming-language interpreter
Last synced: 6 months ago
JSON representation
Simple interpreted imperative language
- Host: GitHub
- URL: https://github.com/pkopel/pig
- Owner: PKopel
- License: other
- Created: 2020-08-05T23:22:25.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-05-04T12:33:24.000Z (12 months ago)
- Last Synced: 2025-05-04T13:32:01.996Z (12 months ago)
- Topics: imperative-programming-language, interpreter
- Language: Haskell
- Homepage:
- Size: 418 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# PiG


Interpreter for a simple language, build with [Alex](https://www.haskell.org/alex/) and [Happy](https://www.haskell.org/happy/).
## Usage
Use
* `stack exec pig` to start interpreter
* `stack install` to install `pig` executable on your machine
### Options
* `--version` show version of interpreter
* `--help`: show options with short description
* `-l|--load FILE`: start interpreter with `FILE` loaded
Command line arguments for scripts are stored in a list of strings named `args`. To distinguish script arguments from intepreter's arguments use `--`, for example with command `pig -l script.pig -- help` string `help` will be available in the script as `args(0)`.
PiG interpreter can also be used with shebang with `#!// -l`, see [example](./examples/hello.pig).
## Language
In PiG, everything is an expression:
* literal values:
* `null`
* `true`/`false`
* numbers (integral and real)
* characters like `'a'` and strings like `"abcd"`
* functions (lambdas) in form `(,...,) => `. Return value of a function is either the value of its last expression or value explicitly marked with the `return` keyword.
* lists in form `[,...,]`
* dictionaries (maps) in form `[: ,...,: = `, where name consists of alphanumeric characters, `() = ` to assign value to a specific element of a list, or `() = ` to add key-value pair to a dictionary. Value of assignment is the value of expression on the right.
* sequence of expressions, separated and optionally ended by `;`. When sequence is enclosed by braces (`{...}`), it is treated as a single expression (sequences can be nested, like `{ ; { ; }; }`). Value of a sequence is the value of the last expression in it.
* while loop: `while : `. Both `` and `` must be a single expressions, but they don't have to be enclosed in braces. `` will be executed as long as `` is a `true`, non-zero number, non-empty list or a non-empty string. Value of a while expression is a list of values of `` (for example value of `x = 3; while x > 0 : x = x - 1` is `[2,1,0]`).
* if: `if : elif : ... else : ` (`elif` and `else` are optional). Value of "if" is the value of expression after ifrst condition evaluated to `true`, non-zero number, non-empty list or a non-empty string, or value of expresion after `else` (`null` if no `else` is specified).
* a variable name, as in assignment. If the variable is bound to a list, `(,...,)` syntax can be used to get the value at specified index or a list of them.
* function application, in form `(,...,)`. If the `` is a list, arguments that can be evaluated to numbers will be rounded to integers and treated as zero-based indices, and the return value will be the value associated with that index or a list of values in case of more than one indices (for example a value of `l = [1,2,3]; l(0)` is `1`, value of `l = [1,2,3]; l(0,2)` is `[1,3]`). If the `` is a dictionary, the return value will be the value associated with the argument or a list of values in case of more than one argument (for example a value of `m = ["a":1,"b":3]; m("a")` is `1`, value of `m = ["a":1,"b":3]; m("a","b")` is `[1,3]`).
* expressions with build-in keywords and operators
Build-in functions provided:
* `print(arg1,...,argn)` prints all its arguments to stdout
* `read()` reads string from stdin
* `open([, ])` opens file and returns its handle
* `close()` closes file
* `readFile()` reads line form file
* `writeFile(,arg1,...,argn)` writes all its arguments except handle to file
* `strToNum(arg)` parses number from string
* `strToList(arg)` turns string to a list of chars
* `listToStr(arg)` turns a list of chars to string
* `length(arg)` returns the length of a list, map or string
* `isNum(arg)` checks if `arg` is a number
* `isBool(arg)` checks if `arg` is a boolean
* `isList(arg)` checks if `arg` is a list
* `isStr(arg)` checks if `arg` is a string
* `isFun(arg)` checks if `arg` is a function
* `exit()` closes interpreter
Keyword `load ""` executes code from other files.
Build-in operators are:
* `+`, `-`, `*`, `/`, `^` and `%` (modulo) for numbers
* `-`, `||`, `&&` for booleans
* `<>`, `-<`, `>-` for lists (`>-` removes and returns first element, `-<` the last one, `<>` concatenates second argument to the end of the first one, works with any type but results always in a list)
* `><` for strings (attaches second argument at the end of the firs one, works with any type but results always in a string)
Interpreter also provides four directives:
* `:exit | :e` (or Ctrl+d) to leave the interpreter
* `:rm ` to remove binding of a variable
* `:clear | :c` to remove all bindings
* `:help | :h` to display information about directives
See [examples](https://github.com/PKopel/PiG/tree/master/examples) for more info.
## Author
* **[Paweł Kopel](https://github.com/PKopel)**