112 lines
2.4 KiB
Markdown
112 lines
2.4 KiB
Markdown
|
# Reference
|
||
|
|
||
|
The following is an informal overview of FSKalc's capabilties.
|
||
|
|
||
|
## Expressions
|
||
|
|
||
|
### Number literal
|
||
|
|
||
|
Example:
|
||
|
|
||
|
```
|
||
|
8.5
|
||
|
```
|
||
|
|
||
|
### Variable read
|
||
|
|
||
|
Example:
|
||
|
|
||
|
```
|
||
|
a
|
||
|
```
|
||
|
|
||
|
Will print an error message if variable is undefined. The `ans` variable implicitly refers to the result of the last expression statement.
|
||
|
|
||
|
### Parentheses for precedence
|
||
|
|
||
|
Example:
|
||
|
|
||
|
```
|
||
|
(5+6)*7
|
||
|
```
|
||
|
|
||
|
Parentheses increase precedence.
|
||
|
|
||
|
### Parentheses for function calls
|
||
|
|
||
|
Parentheses can also represent function calls, if they contain more than one element (separated by a space).
|
||
|
|
||
|
Example:
|
||
|
|
||
|
```
|
||
|
(sin 50)
|
||
|
```
|
||
|
|
||
|
### Arithmetic operators
|
||
|
|
||
|
The following operators are valid, in decreasing precedence:
|
||
|
|
||
|
- `^` power; right associative
|
||
|
- `-` unary negation; note that multiple negations next to eachother are invalid!
|
||
|
- factor precedence:
|
||
|
- `*` multiplication; left associative
|
||
|
- `/` division; left associative
|
||
|
- `//` integer (flooring) division; left associative
|
||
|
- `%` modulo; left associative
|
||
|
- term precedence:
|
||
|
- `+` addition; left associative
|
||
|
- `-` subtraction; left associative
|
||
|
|
||
|
## Statements
|
||
|
|
||
|
Statements are the top level construct. Every line in an FSKalc program has to be a statement.
|
||
|
|
||
|
### Expression statement
|
||
|
|
||
|
Any valid expression is also a valid statement. The result of expression statements is printed to the screen, and also assigned to the variable names `ans`.
|
||
|
|
||
|
### Variable assignments
|
||
|
|
||
|
Variable assignments have the following syntax:
|
||
|
|
||
|
```
|
||
|
identifier = expression
|
||
|
```
|
||
|
|
||
|
Where the identifier is a string of characters containing only letters and underscores.
|
||
|
|
||
|
Existing variables or functions can be overwritten by new variables/functions.
|
||
|
|
||
|
### Function declaration
|
||
|
|
||
|
Function declaration has the following syntax, demonstrated by an example below.
|
||
|
|
||
|
```
|
||
|
double x = x * 2
|
||
|
```
|
||
|
|
||
|
Warning! as there are no scopes in FSKalc, passing
|
||
|
arguments to functions will modify the global scope.
|
||
|
|
||
|
## Builtins
|
||
|
|
||
|
### Builtin Constants
|
||
|
|
||
|
- pi
|
||
|
- tau (pi * 2)
|
||
|
- e (euler's number)
|
||
|
- inf (infinity)
|
||
|
- nan (not a number)
|
||
|
|
||
|
### Builtin Functions
|
||
|
|
||
|
- sin, cos, tan - they take 1 argument, in radians
|
||
|
- arcsin, arccos, arctan - they take 1 argument, they return in radians
|
||
|
- sqrt - returns square root, optional second argument is the power, default of 2
|
||
|
- log - logarithm, optional second argument is the base, default of 10
|
||
|
- ln - natural logarithm
|
||
|
- exp - returns e to the power of the argument
|
||
|
|
||
|
## Comments
|
||
|
|
||
|
Comments can be inside matching `(*`, `*)` parentheses.
|