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
13 changes: 7 additions & 6 deletions ako_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Ako {

// Statement
Block = "{" Stmt* "}"
Stmt = (Metadata | If | Assign | AssignTask | TaskDef | SkipTask | Task | ForLoop | Expr)
Stmt = (Metadata | If | Assign | AssignTask | TaskDef | SkipTask | Task | ForLoop | Expr | comment)

Assign = AssignLeft | AssignAdd | AssignIncr | AssignDecr | AssignSub
AssignTask = Var "=" (SkipTask | Task)
Expand All @@ -23,9 +23,9 @@ Ako {
// Expression
Pipe = (Pipe | ExprItem) "|>" ExprItem
Expr = Pipe | ExprItem
ExprItem = LambdaInline | Fn | MathExpr | Term
ExprItem = LambdaInline | MathExpr

Term = Var | Number | String | bool | Array | Dictionary | Last
Term = Fn | bool | Var | Number | String | Array | Dictionary | Last
Fn = (id ".")? id "(" Arguments ")"
// Lambda = "(" ListOf<id, ","> ")" "=>" Block
LambdaInline = "(" ListOf<id, ","> ")" "=>" Expr
Expand Down Expand Up @@ -81,7 +81,7 @@ Ako {
| Var "." id -- select
| id -- single

id = ~keyword char
id = ~(keyword end) char
char = letter (letter | digit | "_")*
keyword = "$" | "task" | "for" | "false" | "true" | "or" | "and" | "not" | "if" | "elif" | "else" | "continue" | "return"

Expand Down Expand Up @@ -110,7 +110,8 @@ Ako {
hex = "#" hexDigit+

// Other
space := "\t" | " " | "\n" | "\r" | comment | multilinecomment
comment = "//" (~"\n" any)*
space := "\t" | " " | "\n" | "\r"
comment = singlecomment | multilinecomment
singlecomment = "//" (~"\n" any)*
multilinecomment = "/*" (~"*/"any)* "*/"
}
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"cli",
"nocode",
"lowcode",
"interpreter"
"interpreter",
"Ako"
],
"author": {
"name": "Kevin destrem",
Expand Down Expand Up @@ -74,6 +75,7 @@
"typescript": "^4.2.4"
},
"dependencies": {
"chalk": "^4.1.1"
"chalk": "^4.1.1",
"dateformat": "^4.5.1"
}
}
4 changes: 1 addition & 3 deletions samples/fibo.ako
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
## Name "Fibonachi"
## Desc "Compute the fibonachi sequence for a given value"
## Args [
{ name = "val", type = "int", default = 1, description = "" }
]
## Args [{ name = "val", type = "int", default = 1, description = "" }]
## Return "int"

if val <= 0 { return 0 }
Expand Down
4 changes: 3 additions & 1 deletion samples/main.ako
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
start = Time.now()
fibo15 = @fibo(15)
@print("Fibo(15) = {fibo15}")
duration = Time.now() - start
@print("Fibo(15) = {fibo15} (in {duration}ms)")

@Math.main()

Expand Down
4 changes: 3 additions & 1 deletion src/dist/ako-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const {grammar, ASTBuilder} = getGrammar(akoGrammar)

const toAst = (codeTxt: string) => {
const match = grammar.match(codeTxt.toString())
if (!match) throw new Error(`Syntax Error`)
if (!match || match.failed()) {
throw new Error(`Syntax Error ${match.message}`)
}
return ASTBuilder(match).toAST()
}
export {toAst, Interpreter, Analyzer}
9 changes: 9 additions & 0 deletions src/elements/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export const String: ScalarCommandEntry<string> = {
}
}

export const Comment = {
create: (value) => {
return {type: 'Comment', value}
},
execute: (ctx, entry, entryData, time) => {
return {timeRemains: time, done: true}
}
}

export const Number: ScalarCommandEntry<number> = {
create: (value) => {
return {type: 'Number', value}
Expand Down
7 changes: 4 additions & 3 deletions src/semantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function getGrammar(akoGrammar: string) {
const src = start.source.sourceString
const line = src.substring(0, start.source.startIdx).split('\n').length - 1
const sample = src.substring(start.source.startIdx, end.source.endIdx)
const before = src.substring(start.source.startIdx - 15 > 0 ? start.source.startIdx - 15 : 0, start.source.startIdx)
const after = src.substring(end.source.endIdx, end.source.endIdx + 15)
const before = src.substring(start.source.startIdx - 10 > 0 ? start.source.startIdx - 10 : 0, start.source.startIdx)
const after = src.substring(end.source.endIdx, end.source.endIdx + 10)

res.debug = {
line,
Expand Down Expand Up @@ -105,8 +105,9 @@ export function getGrammar(akoGrammar: string) {
Return: (a, b) => debugWrapper(a, b, AkoElement.Return.create(b.toAST())),

// Var
comment: (a) => debugWrapper(a, a, AkoElement.Comment.create(a.sourceString)),
id: (a) => debugWrapper(a, a, AkoElement.String.create(a.sourceString)),
Var_single: (a) => debugWrapper(a, a, AkoElement.Symbol.create(a.toAST())),
Var_single: (a) => debugWrapper(a, a, AkoElement.Symbol.create(a.sourceString)),
Var_select: (a, b, c) => debugWrapper(a, c, AkoElement.SymbolSelect.create(a.toAST(), c.toAST())),
Var_range: (a, b, c, d, e, f) => debugWrapper(a, f, AkoElement.SymbolRange.create(a.toAST(), c.toAST(), e.toAST())),
Var_subscript: (a, b, c, d) => debugWrapper(a, d, AkoElement.SymbolSub.create(a.toAST(), c.toAST())),
Expand Down
2 changes: 2 additions & 0 deletions src/std/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// functions
import {angle, vector2} from './geometry'
import {math} from './math'
import {time} from './time'
import {list} from './collections'
import {string, is} from './scalar'

export const stdFunctions = {
...math,
...time,
...angle,
...vector2,
...list,
Expand Down
18 changes: 18 additions & 0 deletions src/std/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function precisionRound(num: number, precision = 14) {
const factor = Math.pow(10, precision)
return Math.round(num * factor) / factor
}

export const math = {
'Math.PI': (): number => Math.PI,
'Math.max': (...args: number[]): number => Math.max(...args),
'Math.min': (...args: number[]): number => Math.min(...args),
'Math.abs': (arg: number): number => Math.abs(arg),
'Math.ceil': (arg: number): number => Math.ceil(arg),
'Math.floor': (arg: number): number => Math.floor(arg),
'Math.round': (arg: number): number => Math.round(arg),

// random
'Math.rand': (min = 0, max = 1): number => Math.random() * (max - min) + min,
'Math.randint': (min = 0, max = 1): number => Math.floor(Math.random() * (max + 1 - min) + min)
}
18 changes: 0 additions & 18 deletions src/std/math/index.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/std/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import dateFormat from 'dateformat'

export const time = {
'Time.now': (): number => Date.now(),

SECOND: (val = 1) => val * 1000,
MINUTE: (val = 1) => val * 60 * 1000,
HOUR: (val = 1) => val * 60 * 60 * 1000,
DAY: (val = 1) => val * 24 * 60 * 60 * 1000,
WEEK: (val = 1) => val * 7 * 24 * 60 * 60 * 1000,
YEAR: (val = 1) => 365 * 24 * 60 * 60 * 1000,

'Time.day': (d?: number): number => new Date(d ? d : undefined).getDate(),
'Time.weekday': (d?: number): number => new Date(d ? d : undefined).getDay(),
'Time.year': (d?: number): number => new Date(d ? d : undefined).getFullYear(),
'Time.hour': (d?: number): number => new Date(d ? d : undefined).getHours(),
'Time.minute': (d?: number): number => new Date(d ? d : undefined).getMinutes(),
'Time.second': (d?: number): number => new Date(d ? d : undefined).getSeconds(),
'Time.ms': (d?: number): number => new Date(d ? d : undefined).getMilliseconds(),

'Time.parse': (str: string) => Date.parse(str),
'Time.format': (d: number, format = 'yyyy-mm-dd HH:MM:ss') => dateFormat(new Date(d), format),
'Time.tz': () => new Date().getTimezoneOffset()
}
7 changes: 6 additions & 1 deletion tests/elements/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ a = unknown(1,2,3,4,5)
it('Check Expression Function', () => {
const {stack} = runCode(`
a = Math.max(1,2,3,4,5)

b = Math.max(1,2,3,4,5) - 2
c = 2 - Math.max(1,2,3,4,5)
d = (Math.max(1,2,3,4,5) + 2) % 5
`)
assert.strictEqual((stack.data as any)['a'], 5)
assert.strictEqual((stack.data as any)['b'], 3)
assert.strictEqual((stack.data as any)['c'], -3)
assert.strictEqual((stack.data as any)['d'], 2)
})

it('Vector', () => {
Expand Down
66 changes: 66 additions & 0 deletions tests/std/time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import assert from 'assert'
import {runCode} from '../helper'

describe('Time', () => {
it('now', () => {
const {stack: stack} = runCode(`
a = Time.now()
`)
assert.strictEqual(Date.now() - (stack.data as any)['a'] < 5, true)
})

it('parse', () => {
const {stack: stack} = runCode(`
b = Time.parse('2014-07-03')
c = Time.parse('2014-07-03') |> $ + HOUR(5) |> Time.format($, 'yyyy-mm-dd')
`)
assert.strictEqual((stack.data as any)['b'], Date.parse('2014-07-03'))
assert.strictEqual((stack.data as any)['c'], '2014-07-03')
})

it('getter', () => {
const {stack: stack} = runCode(`
date = Time.parse('2014-07-03 17:10:22')

a = Time.day(date) // get day 0-31
b = Time.weekday(date) // get day in the week 0-6
c = Time.year(date) // get year 2014
d = Time.hour(date) // get hour 0-23
e = Time.minute(date) // get minute 0-59
f = Time.second(date) // get second 0-59
g = Time.ms(date) // get second 0-999
h = Time.tz() // get timezone offset in minute
i = Time.format(date, 'yyyy-mm-dd HH:MM:ss')
`)

assert.strictEqual((stack.data as any)['a'], 3)
assert.strictEqual((stack.data as any)['b'], 4)
assert.strictEqual((stack.data as any)['c'], 2014)
assert.strictEqual((stack.data as any)['d'], 17)
assert.strictEqual((stack.data as any)['e'], 10)
assert.strictEqual((stack.data as any)['f'], 22)
assert.strictEqual((stack.data as any)['g'], 0)
assert.strictEqual((stack.data as any)['h'], new Date().getTimezoneOffset())
assert.strictEqual((stack.data as any)['i'], '2014-07-03 17:10:22')
})

it('getter', () => {
const {stack: stack} = runCode(`
date = Time.parse('2014-07-03 12:01:02')

d1 = Time.format(date + SECOND(10))
d2 = Time.format(date + MINUTE(10))
d3 = Time.format(date + HOUR(24))
d4 = Time.format(date + DAY(24))
d5 = Time.format(date + WEEK())
d6 = Time.format(date + YEAR())
`)

assert.strictEqual((stack.data as any)['d1'], '2014-07-03 12:01:12')
assert.strictEqual((stack.data as any)['d2'], '2014-07-03 12:11:02')
assert.strictEqual((stack.data as any)['d3'], '2014-07-04 12:01:02')
assert.strictEqual((stack.data as any)['d4'], '2014-07-27 12:01:02')
assert.strictEqual((stack.data as any)['d5'], '2014-07-10 12:01:02')
assert.strictEqual((stack.data as any)['d6'], '2015-07-03 12:01:02')
})
})