script

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 6 Imported by: 0

README

GoScript

Lightweight go-like scripting language.

Package


var source, _ = os.ReadFile(path)
script.Compile(string(source)).Run(storage)

Command


go-script [args...]

Syntax


func () {}
new {}
if true {}
for true {}
{}

value ()
value . key
key = value

true
false
key
0.0
""
''

Examples


main = func () {
    s = slice(1.2 2.3)
    sum = this.sum()
    fmt.Println(sum)
    // Output:
    // 3.5
}
sum = func (s) {
    i = 0
    sum = 0
    for lt(i len(s)) {
        sum = add(sum get(s i))
        i = add(i 1)
    }
    return(sum)
}
async = func () {
    msgs = chan(0)
    go(func () {
        for true {
            msg = recv(this.msgs)
            if eq(msg "exit") {
                fmt.Println("server exited")
                break()
            }
            fmt.Println(msg)
        }
    })
    for true {
        msg = fmt.Scanln()
        if eq(msg "bye") {
            fmt.Println("client exited")
            break()
        }
        send(msgs msg)
    }
    // Output:
    // 123
    // 123
    // exit
    // server exited
    // bye
    // client exited
}
object = func () {
    User = func (id name) {
        user = new {
            id = this.id
            name = this.name
            work = func () {
                fmt.Println("I'm working, and my name is" this.name)
            }
        }
        return(user)
    }
    User(1 "Tom").work()
    t = time.Now()
    fmt.Println(t.Year())
    fmt.Println(t.Format("2006-01-02 15:04:05"))
    // Output:
    // I'm working, and my name is Tom
    // 2024
    // 2024-03-27 08:23:27
}
closure = func () {
    inc = func () {
        val = add(this.val 10)
        this.val = 30
        return(val)
    }
    val = 10
    fmt.Println(inc())
    fmt.Println(val)
    // Output:
    // 20
    // 30
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct{ Statements []Script }

func (*Block) Run

func (b *Block) Run(storage map[any]any) any

type Call

type Call struct {
	Key  Script
	Args []Script
}

func (*Call) Run

func (c *Call) Run(storage map[any]any) any

type Constant

type Constant struct{ Value any }

func (*Constant) Run

func (c *Constant) Run(storage map[any]any) any

type For

type For struct {
	Condition, Body Script
}

func (*For) Run

func (f *For) Run(storage map[any]any) any

type Func

type Func struct {
	Keys []string
	Body Script
}

func (*Func) Run

func (f *Func) Run(storage map[any]any) any

type If

type If struct{ Condition, Body Script }

func (*If) Run

func (i *If) Run(storage map[any]any) any

type New

type New struct{ Body Script }

func (*New) Run

func (n *New) Run(storage map[any]any) any

type Script

type Script interface{ Run(storage map[any]any) any }

func Compile

func Compile(source string) Script

type Selector

type Selector struct {
	Value Script
	Key   string
}

func (*Selector) Run

func (s *Selector) Run(storage map[any]any) any

type Store

type Store struct {
	Key, Value Script
}

func (*Store) Run

func (s *Store) Run(storage map[any]any) any

type Variable

type Variable struct{ Key string }

func (*Variable) Run

func (v *Variable) Run(storage map[any]any) any

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL