2021-03-02 01:07:35 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2021 Arsen Musayelyan
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
*/
|
|
|
|
|
2021-03-01 17:11:22 +00:00
|
|
|
package scpt
|
|
|
|
|
2021-03-01 17:22:00 +00:00
|
|
|
import (
|
2021-03-02 00:44:33 +00:00
|
|
|
"errors"
|
2021-03-01 21:43:10 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/alecthomas/participle/lexer"
|
2021-03-01 17:22:00 +00:00
|
|
|
)
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// AST stores the root of the Abstract Syntax Tree for scpt
|
2021-03-01 17:11:22 +00:00
|
|
|
type AST struct {
|
2021-03-01 21:43:10 +00:00
|
|
|
Pos lexer.Position
|
2021-03-01 17:11:22 +00:00
|
|
|
Commands []*Command `@@*`
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Execute traverses the AST and executes any commands, it returns an error
|
|
|
|
// containing the position at which the error was encountered and the error
|
|
|
|
// itself
|
|
|
|
func (ast *AST) Execute() error {
|
2021-03-01 23:01:21 +00:00
|
|
|
// For each command in AST
|
2021-03-01 17:22:00 +00:00
|
|
|
for _, cmd := range ast.Commands {
|
2021-03-02 03:43:06 +00:00
|
|
|
// Execute current command
|
|
|
|
err := executeCmd(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and execute command
|
|
|
|
func executeCmd(cmd *Command) error {
|
|
|
|
// If parsing variables
|
|
|
|
if cmd.Vars != nil {
|
|
|
|
// For each variable
|
|
|
|
for _, Var := range cmd.Vars {
|
|
|
|
// Parse value of variable
|
|
|
|
val, err := ParseValue(Var.Value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", Var.Value.Pos, err)
|
|
|
|
}
|
|
|
|
// If value of variable is a function call
|
|
|
|
if IsFuncCall(val) {
|
|
|
|
// Assert type of val as *FuncCall
|
|
|
|
Call := val.(*FuncCall)
|
|
|
|
// Set variable value to function return value
|
|
|
|
Vars[Var.Key], err = CallFunction(Call)
|
2021-03-01 21:43:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", Var.Value.Pos, err)
|
|
|
|
}
|
2021-03-02 03:43:06 +00:00
|
|
|
} else {
|
|
|
|
// If value is not a function call, set variable value to parsed value
|
|
|
|
Vars[Var.Key] = val
|
2021-03-01 17:22:00 +00:00
|
|
|
}
|
2021-03-02 03:43:06 +00:00
|
|
|
}
|
|
|
|
} else if cmd.Calls != nil {
|
|
|
|
// For each function call
|
|
|
|
for _, Call := range cmd.Calls {
|
|
|
|
// Attempt to call function
|
|
|
|
_, err := CallFunction(Call)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", Call.Pos, err)
|
2021-03-01 17:22:00 +00:00
|
|
|
}
|
2021-03-02 03:43:06 +00:00
|
|
|
}
|
|
|
|
} else if cmd.Ifs != nil {
|
|
|
|
// For each if statement
|
|
|
|
for _, If := range cmd.Ifs {
|
|
|
|
// Get condition value
|
|
|
|
condVal, err := callIfFunc(ParseValue(If.Condition))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", If.Condition.Pos, err)
|
|
|
|
}
|
|
|
|
// Attempt to assert condition type as bool
|
|
|
|
condBool, ok := condVal.(bool)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("condition must be a boolean")
|
|
|
|
}
|
|
|
|
// If condition is true
|
|
|
|
if condBool {
|
|
|
|
// For each inner command
|
|
|
|
for _, InnerCmd := range If.InnerCmds {
|
|
|
|
// Execute command recursively
|
|
|
|
err := executeCmd(InnerCmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", InnerCmd.Pos, err)
|
|
|
|
}
|
2021-03-02 00:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 17:22:00 +00:00
|
|
|
}
|
2021-03-02 04:18:52 +00:00
|
|
|
} else if cmd.RptLoops != nil {
|
|
|
|
// For each repeat loop
|
|
|
|
for _, RptLoop := range cmd.RptLoops {
|
|
|
|
for i:=0;i<*RptLoop.Times;i++ {
|
|
|
|
if RptLoop.IndexVar != nil {
|
|
|
|
Vars[*RptLoop.IndexVar] = i
|
|
|
|
}
|
|
|
|
for _, InnerCmd := range RptLoop.InnerCmds {
|
|
|
|
// Execute command recursively
|
|
|
|
err := executeCmd(InnerCmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", InnerCmd.Pos, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 17:22:00 +00:00
|
|
|
}
|
2021-03-01 21:43:10 +00:00
|
|
|
return nil
|
2021-03-01 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Command stores any commands encountered while parsing a script
|
2021-03-01 17:11:22 +00:00
|
|
|
type Command struct {
|
2021-03-02 04:18:52 +00:00
|
|
|
Pos lexer.Position
|
2021-03-02 03:43:06 +00:00
|
|
|
Tokens []lexer.Token
|
2021-03-02 04:18:52 +00:00
|
|
|
Vars []*Var `( @@`
|
|
|
|
Ifs []*If `| @@`
|
|
|
|
RptLoops []*RptLoop`| @@`
|
|
|
|
Calls []*FuncCall `| @@)`
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 03:43:06 +00:00
|
|
|
// If stores any if statements encountered while parsing a script
|
2021-03-02 00:44:33 +00:00
|
|
|
type If struct {
|
|
|
|
Pos lexer.Position
|
2021-03-02 03:43:06 +00:00
|
|
|
Condition *Value `"if" @@ "{"`
|
2021-03-02 04:18:52 +00:00
|
|
|
InnerCmds []*Command `@@* "}"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RptLoop struct {
|
|
|
|
Pos lexer.Position
|
|
|
|
Times *int `"repeat" @Number "times" "{"`
|
|
|
|
IndexVar *string `(@Ident "in")?`
|
|
|
|
InnerCmds []*Command `@@* "}"`
|
2021-03-02 00:44:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// FuncCall stores any function calls encountered while parsing a script
|
2021-03-01 17:11:22 +00:00
|
|
|
type FuncCall struct {
|
2021-03-01 21:43:10 +00:00
|
|
|
Pos lexer.Position
|
|
|
|
Name string `@Ident @("-" Ident)*`
|
|
|
|
Args []*Arg `@@*`
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Arg stores arguments for function calls
|
2021-03-01 17:11:22 +00:00
|
|
|
type Arg struct {
|
2021-03-01 21:43:10 +00:00
|
|
|
Pos lexer.Position
|
|
|
|
Key string `("with" @Ident)?`
|
|
|
|
Value *Value `@@`
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Var stores any variables encountered while parsing a script
|
2021-03-01 17:11:22 +00:00
|
|
|
type Var struct {
|
2021-03-01 21:43:10 +00:00
|
|
|
Pos lexer.Position
|
|
|
|
Key string `"set" @Ident "to"`
|
|
|
|
Value *Value `@@`
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Value stores any literal values encountered while parsing a script
|
2021-03-01 17:11:22 +00:00
|
|
|
type Value struct {
|
2021-03-02 04:18:52 +00:00
|
|
|
Pos lexer.Position
|
|
|
|
String *string ` @String`
|
|
|
|
Number *float64 `| @Number`
|
|
|
|
Bool *Bool `| @("true" | "false")`
|
|
|
|
SubCmd *FuncCall `| "(" @@ ")"`
|
|
|
|
VarVal *string `| "$" @Ident`
|
|
|
|
Expr *Expression `| "{" @@ "}"`
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Bool stores boolean values encountered while parsing a script.
|
|
|
|
// It is required for the Capture method
|
2021-03-01 17:11:22 +00:00
|
|
|
type Bool bool
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Capture parses a boolean literal encountered in the script into
|
|
|
|
// a Go boolean value
|
2021-03-01 17:11:22 +00:00
|
|
|
func (b *Bool) Capture(values []string) error {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Convert string to boolean
|
2021-03-01 17:11:22 +00:00
|
|
|
*b = values[0] == "true"
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-01 21:43:10 +00:00
|
|
|
|
|
|
|
// Expression stores any expressions encountered while parsing a
|
|
|
|
// script for later evaluation
|
|
|
|
type Expression struct {
|
2021-03-02 00:44:33 +00:00
|
|
|
Pos lexer.Position
|
|
|
|
Left *Value `@@`
|
|
|
|
RightSegs []*ExprRightSeg `@@*`
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:43:06 +00:00
|
|
|
// ExprRightSeg stores segments of the right side of an expression
|
2021-03-02 00:44:33 +00:00
|
|
|
type ExprRightSeg struct {
|
2021-03-02 03:43:06 +00:00
|
|
|
Op string `@Operator`
|
2021-03-01 21:43:10 +00:00
|
|
|
Right *Value `@@`
|
|
|
|
}
|