Go to file
Arsen Musayelyan 31c3a2f3a9 Add ability to define functions with dashes in name 2021-04-29 23:14:52 -07:00
cmd/scpt Remove unnecessary copyright headers 2021-03-08 11:28:47 -08:00
.gitignore Implement functions, arrays, maps, and while loops. Document and clean up code. 2021-03-04 19:30:08 -08:00
LICENSE Add LICENSE 2021-03-01 12:31:22 -08:00
README.md Add defer file.Close() to the README interpreter example 2021-03-08 11:35:15 -08:00
ast.go Add ability to define functions with dashes in name 2021-04-29 23:14:52 -07:00
defaults.go Add goroutines 2021-04-05 11:28:47 -07:00
go.mod Clean up code and add support for multiple operations in expression 2021-03-01 16:44:33 -08:00
go.sum Add syntax highlighting file 2021-03-24 10:54:10 -07:00
lexer.go Remove unnecessary copyright headers 2021-03-08 11:28:47 -08:00
scpt.go Remove unnecessary copyright headers 2021-03-08 11:28:47 -08:00
scpt.yaml Add syntax highlighting file 2021-03-24 10:54:10 -07:00
test.scpt Add ability to define functions with dashes in name 2021-04-29 23:14:52 -07:00

README.md

scpt

scpt is an applescript-inspired scripting language written for fun and to see if I could.

Go Reference


Usage

scpt is to be used as a library imported into Go. A basic interpreter with no extra functionality would look like this:

package main

import (
	"gitea.arsenm.dev/Arsen6331/scpt"
	"log"
	"os"
	"path/filepath"
)

func main() {
	filename := os.Args[1]
	file, err := os.Open(filepath.Clean(filename))
	if err != nil {
		log.Fatalln(err)
	}
	defer file.Close()
	ast, err := scpt.Parse(file)
	if err != nil {
		log.Fatalln(err)
	}
	err = ast.Execute()
	if err != nil {
		log.Fatalln(err)
	}
}

Basic Syntax

The basic syntax of scpt can be learned from the test.scpt file.


Default Functions

scpt comes with the following default functions:

  • str: Convert value to string
  • num: Parse string to number (float64)
  • bool: Parse string to boolean
  • break: Break out of loop (Errors if not in loop)
  • append: Return an array with given items appended
  • exit: Exit with given exit code
  • return: Return value in function (Errors if not within function)
  • print: Print using fmt.Println()

Adding functionality:

Adding functionality is simple and requires a call to scpt.AddFuncs() or scpt.AddVars(). Here are some examples:

scpt.AddFuncs(scpt.FuncMap{
	"my-function": myFunction
})

Where myFunction is:

func myFunction(args map[string]interface{}) (interface{}, error) {
	fmt.Println(args)
	return nil, nil
}

After the call to scpt.AddFuncs(), my-function can be used to run the function from within an scpt script. Variables work similarly.