Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* [Home](/)
* [Grammar](./docs/grammar_basic.md)
* [Basic](./docs/grammar_basic.md)
* [Expression](./docs/grammar_expression.md)
* [Loop](./docs/grammar_loop.md)
* [Task](./docs/grammar_task.md)
* [Interpreter](./docs/interpreter_basic.md)
Expand Down
20 changes: 4 additions & 16 deletions docs/grammar_basic.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Basic
# How AKO works?

The key element of Ako language is to represent algorithm as a succession of separated Commands.
The key element of **Ako** is to represent algorithm as a succession of separated Commands

And there is only a really limited set of possible Commands. The more commons are:
* Assign a variable with: `=`
* Assign an expression result to a variable with: `=`
* Conditional with: `If`
* Loop with: `For`
* Execute a task with: `@`
Expand All @@ -26,8 +26,7 @@ fibo15 = @fibo(15)
@print("Fibo(15) = {fibo15}")
```

---
## Basic
# Basic Types

### Variable

Expand Down Expand Up @@ -75,14 +74,3 @@ list = [1, 2, 3, 4]
// Create dictionary
pos = { x = 1, y = 2, z = 3 }
```

## Expression
```js
num = 2 + 1
float = num * 1.5
```

### Expression using function
```js
num = Math.max(2 + 1, 2 * 2)
```
59 changes: 59 additions & 0 deletions docs/grammar_expression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Expression

Expression are a major part of **Ako**, the can be see as Excel formula.
It can be:
* scalar (string, boolean, number, array, ...)
* variable
* mathematic operation
* function call

but it can also be a mix of those and contain parentheses `Math.abs(-2+1) * 2`

## Math Expression
```js
num = 2 + 1
float = num * 1.5
```

List of mathematical support
* `*` : Multiply
* `/` : Divide
* `+` : Add
* `-` : Substract
* `%` : Modulo

## Expression using function
```js
num = Math.max(2 + 1, 2 * 2)
```

## Expression Pipe
Expression can become quite long and hard to read, the Pipe operator is there to help chaining function call. It take the result of an expr and pass it to another one

```js
a = List.sort(List.map(List.filter([1,2,4,5,6], (val) => val < 5), (val) => 10 / val))

// can be rewritten into
a = [1,2,4,5,6]
|> List.filter($, (val) => val < 5)
|> List.map($, (val) => 10 / val)
|> List.sort($)
```

# Equality Expression
```js
a = true
b = a * 2 >= 12
```

List of mathematical support
* `==` : Equal
* `!=` : Different
* `<` : Less than
* `<=` : Less or equal than
* `>` : More than
* `>=` : More or equal than

And they can be combined logic operator
* `and`
* `or`
4 changes: 3 additions & 1 deletion docs/grammar_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ A task is an operation that can take a certain amount of time to do things:
* Playing animations
* Waiting for user inputs

In other language it's often known as `job` or `coroutine`.

And in Ako scripts, they are called with a `@` in front

```js
Expand Down Expand Up @@ -80,4 +82,4 @@ task hello {
}

@hello("World")
```
```
7 changes: 5 additions & 2 deletions src/ako_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ Ako {
Arguments = ListOf<Expr, ",">

// Expression
Expr = LambdaInline | Fn | MathExpr | Term
Term =Var | Number | String | bool | Array | Dictionary | Last
Pipe = (Pipe | ExprItem) "|>" ExprItem
Expr = Pipe | ExprItem
ExprItem = LambdaInline | Fn | MathExpr | Term

Term = Var | Number | String | bool | Array | Dictionary | Last
Fn = (id ".")? id "(" Arguments ")"
// Lambda = "(" ListOf<id, ","> ")" "=>" Block
LambdaInline = "(" ListOf<id, ","> ")" "=>" Expr
Expand Down
11 changes: 11 additions & 0 deletions src/elements/operator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {precisionRound} from '../std/math'

export const Pipe = {
create: (expr1, expr2) => {
return {type: 'Pipe', expr1, expr2}
},
evaluate: (ctx, entry) => {
const val1 = ctx.vm.evaluate(ctx, entry.expr1, true)
ctx.vm.setData(ctx, '$', val1)
return ctx.vm.evaluate(ctx, entry.expr2, true)
}
}

export const Operator = {
create: (operator, expr1, expr2) => {
return {type: 'Operator', operator, expr1, expr2}
Expand Down
1 change: 1 addition & 0 deletions src/semantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function getGrammar(akoGrammar: string) {
Fn: (a, b, c, d, e, f) => AkoElement.Function.create(a.toAST(), c.toAST(), e.toAST()),
Arguments: (a) => a.asIteration().toAST(),
ListOf: (a) => a.asIteration().toAST(),
Pipe: (a, b, c) => AkoElement.Pipe.create(a.toAST(), c.toAST()),
Metadata: (a, b, c) => AkoElement.Metadata.create(b.toAST(), c.toAST()),

// Assign
Expand Down
15 changes: 15 additions & 0 deletions tests/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ c = Vec2.sub(Vec2.scale(a, 5), Vec2.create(5, 5))
assert.deepStrictEqual((stack.data as any)['c'], [0, 0])
})
})

describe('Pipe', function () {
it('Check Pipe Behavior', () => {
const {stack} = runCode(`
b = List.sort(List.map(List.filter([1,2,4,5,6], (val) => val < 5), (val) => 10 / val))

a = [1,2,4,5,6]
|> List.filter($, (val) => val < 5)
|> List.map($, (val) => 10 / val)
|> List.sort($)
`)
assert.deepStrictEqual((stack.data as any)['b'], [10 / 4, 10 / 2, 10])
assert.deepStrictEqual((stack.data as any)['a'], [10 / 4, 10 / 2, 10])
})
})