Skip to content

A Go package implementation of real-time computing

License

Notifications You must be signed in to change notification settings

shockerli/spiker

Repository files navigation

Spiker

PkgGoDev Go Report Card codecov GitHub top language

A Go package implementation of real-time computing

lexer is inspired by tdop

English | 中文

Install

go get -u github.com/shockerli/spiker

Usage

  • Execute
spiker.Execute(`1 + 2 * 3 / 4`)
  • ExecuteWithScope
var scopes = spiker.NewScopeTable("demo", 1, nil)
scopes.Set("a", 3)
scopes.Set("b", 4)

spiker.ExecuteWithScope(`a * 2 + b`, )
  • Format
spiker.Format(`a + b * 3`)

Architecture

architecture

Examples

Keywords

true/false/if/else/in/...

Instruction separation

Spiker requires instructions to be terminated with a semicolon at the end of each statement

Data type

  • Number

Integer, Float

123;
-123;
12.34;
-12.34;
  • String
"abc"
  • Boolean
true;
false;
  • Array
[];
[1, 2, "a"];
[1, [], [2,], [3, 4,], 5];
  • Dict
v = [9:99, 8:8.8, "hello":12.02];
v = ["name":"jioby", "age":18, "log":[1:2, 3:4]];
v = [1, 9:9.9, 3];

Arithmetic Operators

1 + 2 - 3 * 4 / 5 % 6;
-0.19 + 3.2983;
3 ** 2;

Bitwise Operators

1 & 2;
1 | 2;
1 ^ 2;
1 >> 2;
1 << 2;

Comparison Operators

3 == 2;
3 != 2;
3 > 2;
3 >= 2;
3 < 2;
3 <= 2;

Logical Operators

!2;
1 && 2;
1 || 2;

Assignment Operators

v = 2;
v += 2;
v -= 2;
v *= 2;
v /= 2;
v %= 2;

a = b = c = 100;

In Operator

"john" in ["joy", "john"]; // true
100 in [100, 200, 300]; // true
9 in [9:"999", 8:"888"]; // true
9 in "123456789"; // true

Build-in functions

  • export

return the expression value and interrupt script

export(v);
export(v[i]);
  • exist

determines whether a variable or index exists

exist(v);
exist(v[i]);
  • len

return the length of a value

len("123");
len(v);
len(v[i]);
  • del

delete one or more variable or index

del(a)
del(a, b)
del(a, b[1])
del(a[i], b[i])

Custom function

  • single
sum = (a, b) -> a + b;

export(sum(1, 2)); # 3
pow2 = x -> x ** 2;

export(pow2(5)); # 25
  • block
max = (a, b) -> {
    if (a > b) {
        return a;
    } else {
        return b;
    }
};

export(max(1, 2)); # 2

Control structures

  • if/else
if (a > b && c != d) {
    a = b;
} else if (c > b) {
    a = c;
} else {
    export(c + d);
}
  • while
while (true) {
  a = 0;
  while (a < 10) {
    print(a, "\n");
    a += 1;
  }
}

More

a = 101;
b = 102;
c = "103";
d = [1, 2, a, b, c+b, "abc"];

if (a > b) {
    export(len(d));
} else if (a < b) {
    if (c) {
        d = -1000;
    } else {
        d = 0;
    }

    d += len(c);
    export(d);
}

export(d[2]);

License

This project is licensed under the terms of the MIT license.