Giter VIP home page Giter VIP logo

gscript's Introduction

The Gscript Language

English|中文

Gscript is a light, dynamic script language written in Go.

example:

import fs
import os

# define a function
func writeSomeMsg(filepath) {
    # open, create and truncate text.txt in write-only mode
    let file = fs.open(filepath,"wct");

    # write some message into file
    file.write("hello world!\n")
    file.write("gscript is a good language!")

    # close file
    file.close();
}

let filepath = "./text.txt"

try{
    writeSomeMsg(filepath)

    let stat = fs.stat(filepath)
    print("size of text.txt is " + stat.size + "B");
    
    # read all data from text.txt
    let data = fs.readFile(filepath);
    print("message of", filepath, "is:")
    print(data.toString())

    # remove text.txt
    fs.remove(filepath)
    print("success!")
}
catch(e){
    print("operation failed, error msg:",e)
    os.exit(0)
}

Features

  • Function

    • Multiple return values

    • Closure

    • Recursive function call

  • All standard libraries are packaged into one executable, which means, to install or use gscript it's no need to configure some environment variables.

  • Object-oriented programming

  • Debug mode

  • Can generate human-readable assemble-like codes

  • Simple syntax, easy to learn, especially to coders with JavaScript or Python experiences.

  • Modular support

  • Exception support: try, catch and throw

  • Complied/executed as bytecode on stack-based VM

Install

Since compiler and VM are written in pure Go(no cgo) and does not use any platform-dependent interfaces, so Gscript is a cross-platform language.

You can install Gscript in two ways:

  • Compile from source code.

    note: we use the new feature "embed" of Go 1.16, so make sure your Go version is greater than or equal to 1.16.

    git clone [email protected]:gufeijun/gscript.git
    cd gscript
    sh build.sh

    then the compiler will generated to bin/gsc.

  • Download from releases.

gsc means gscript compiler. You can add the executable to PATH as you wish.

Then all you need is just having fun.

Quick Start

open file main.gs and write codes below:

print("hello world");

run the script:

gsc run main.gs

you will get following output:

hello world

you can also do something more challenging:

print(fib(20))

func fib(x) {
    if (x == 0) return 0;
    if (x == 1) return 1;
    return fib(x-1) + fib(x-2)
}

run the script, you will get 6765.

Usage

we demonstrated the command run of gsc above. You can use gsc --help for more details about how to use gsc.

  • use gsc debug <source file> or gsc debug <bytecode file>to enter debug mode, in which we can execute virtual instruction one by one and view stack and variable table changes in real time.

  • use gsc build <source file> to generate bytecode, besides, you can use -o flag to specific name of output file.

  • use gsc build -a <source file> or gsc build -a <bytecode file> to generate human-readable assemble-like codes.

    Take the main.gs in section Quick Start as an example, run the following command:

    gsc build -a main.gs -o output.gscasm

    It will generate output.gsasm:

    /home/xxx/gscript/main.gs(MainProto):
    	MainFunc:
    		0		LOAD_CONST "hello world"
    		9		LOAD_BUILTIN "print"
    		14		CALL 0 1
    		17		STOP
    
  • use gsc run <source file> or gsc run <bytecode file> to run the script.

References

gscript's People

Contributors

gufeijun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

adamking07

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.