Skip to content

Refractoring #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lang/builtins/builtins.go → builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ package builtins
// first take a look at `misc`.

import (
_ "github.com/lmorg/murex/lang/builtins/encoders"
_ "github.com/lmorg/murex/lang/builtins/httpclient"
_ "github.com/lmorg/murex/lang/builtins/io"
_ "github.com/lmorg/murex/lang/builtins/management"
_ "github.com/lmorg/murex/lang/builtins/misc"
_ "github.com/lmorg/murex/lang/builtins/structs"
_ "github.com/lmorg/murex/lang/builtins/textmanip"
_ "github.com/lmorg/murex/lang/builtins/typemgmt"
_ "github.com/lmorg/murex/builtins/encoders"
_ "github.com/lmorg/murex/builtins/httpclient"
_ "github.com/lmorg/murex/builtins/io"
_ "github.com/lmorg/murex/builtins/management"
_ "github.com/lmorg/murex/builtins/misc"
_ "github.com/lmorg/murex/builtins/structs"
_ "github.com/lmorg/murex/builtins/textmanip"
_ "github.com/lmorg/murex/builtins/typemgmt"
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func cmdEscape(p *proc.Process) error {

}

if p.Not {
if p.IsNot {
unescape, err := strconv.Unquote(str)
if err != nil {
unescape = html.UnescapeString(str)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func cmdIf(p *proc.Process) (err error) {
var ifBlock, thenBlock, elseBlock []rune

switch {
case p.Parameters.Len() == 1 && p.Method:
case p.Parameters.Len() == 1 && p.IsMethod:
// "if" taken from stdin, "then" from 1st parameter.
thenBlock, err = p.Parameters.Block(0)
if err != nil {
return err
}

case p.Parameters.Len() == 2 && p.Method:
case p.Parameters.Len() == 2 && p.IsMethod:
// "if" taken from stdin, "then" and "else" from 1st and 2nd parameter.
thenBlock, err = p.Parameters.Block(0)
if err != nil {
Expand All @@ -36,7 +36,7 @@ func cmdIf(p *proc.Process) (err error) {
return err
}

case p.Parameters.Len() == 2 && !p.Method:
case p.Parameters.Len() == 2 && !p.IsMethod:
// "if" taken from 1st parameter, "then" from 2nd parameter.
ifBlock, err = p.Parameters.Block(0)
if err != nil {
Expand All @@ -48,7 +48,7 @@ func cmdIf(p *proc.Process) (err error) {
return err
}

case p.Parameters.Len() == 3 && !p.Method:
case p.Parameters.Len() == 3 && !p.IsMethod:
// "if" taken from 1st parameter, "then" from 2nd, "else" from 3rd.
ifBlock, err = p.Parameters.Block(0)
if err != nil {
Expand All @@ -66,7 +66,7 @@ func cmdIf(p *proc.Process) (err error) {
}

default:
if !p.Not {
if !p.IsNot {
return errors.New(`Not a valid if statement. Usage:
$conditional -> if: { $then } # conditional result read from stdin or previous process exit number
$conditional -> if: { $then } { $else } # conditional result read from stdin or previous process exit number
Expand Down Expand Up @@ -101,7 +101,7 @@ func cmdIf(p *proc.Process) (err error) {
conditional = types.IsTrue(b, p.Previous.ExitNum)
}

if (conditional && !p.Not) || (!conditional && p.Not) {
if (conditional && !p.IsNot) || (!conditional && p.IsNot) {
// --- THEN ---
_, err = lang.ProcessNewBlock(thenBlock, nil, p.Stdout, p.Stderr, types.Null)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func cmdCatch(p *proc.Process) error {

p.ExitNum = p.Previous.ExitNum

if p.Previous.ExitNum != 0 && !p.Not {
if p.Previous.ExitNum != 0 && !p.IsNot {
_, err = lang.ProcessNewBlock(block, nil, p.Stdout, p.Stderr, types.Null)
if err != nil {
return err
}

} else if p.Previous.ExitNum == 0 && p.Not {
} else if p.Previous.ExitNum == 0 && p.IsNot {
_, err = lang.ProcessNewBlock(block, nil, p.Stdout, p.Stderr, types.Null)
if err != nil {
return err
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func cmdMatch(p *proc.Process) error {

p.Stdin.ReadLineFunc(func(b []byte) {
matched := bytes.Contains(b, p.Parameters.ByteAll())
if (matched && !p.Not) || (!matched && p.Not) {
if (matched && !p.IsNot) || (!matched && p.IsNot) {
p.Stdout.Write(b)
}
})
Expand Down Expand Up @@ -61,7 +61,7 @@ func cmdRegexp(p *proc.Process) (err error) {
//}
p.Stdin.ReadLineFunc(func(b []byte) {
matched := rx.Match(b)
if (matched && !p.Not) || (!matched && p.Not) {
if (matched && !p.IsNot) || (!matched && p.IsNot) {
p.Stdout.Write(b)
}
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func cmdSet(p *proc.Process) error {
params := p.Parameters.StringAll()

// Set variable as method:
if p.Method {
if p.IsMethod {
if !rxVarName.MatchString(params) {
return errors.New("Invalid variable name; unexpected parameters for calling `set` as method.")
}
Expand All @@ -65,7 +65,7 @@ func cmdExport(p *proc.Process) error {
params := p.Parameters.StringAll()

// Set env as method:
if p.Method {
if p.IsMethod {
if !rxVarName.MatchString(params) {
return errors.New("Invalid variable name; unexpected parameters for calling `export` as method.")
}
Expand Down
6 changes: 3 additions & 3 deletions lang/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/lmorg/murex/lang/proc/parameters"
)

type Node struct {
type astNode struct {
Name string
ParamTokens [][]parameters.ParamToken
NewChain bool
Expand All @@ -16,9 +16,9 @@ type Node struct {
//Children Nodes
}

type Nodes []Node
type astNodes []astNode

func (n *Nodes) Last() *Node {
func (n *astNodes) Last() *astNode {
if len(*n) == 0 {
return &(*n)[0]
}
Expand Down
24 changes: 13 additions & 11 deletions lang/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/lmorg/murex/lang/proc/streams"
)

func compile(tree *Nodes, parent *proc.Process) {
func compile(tree *astNodes, parent *proc.Process) {
for i := range *tree {
(*tree)[i].Process.Name = (*tree)[i].Name
(*tree)[i].Process.Parameters.SetTokens((*tree)[i].ParamTokens)
(*tree)[i].Process.Method = (*tree)[i].Method
(*tree)[i].Process.IsMethod = (*tree)[i].Method
(*tree)[i].Process.Parent = parent

// Define previous and next processes:
Expand Down Expand Up @@ -77,7 +77,7 @@ func compile(tree *Nodes, parent *proc.Process) {
}

for i := range *tree {
proc.CreateProcess(&(*tree)[i].Process, proc.Flow{
createProcess(&(*tree)[i].Process, proc.Flow{
NewChain: (*tree)[i].NewChain,
PipeOut: (*tree)[i].PipeOut,
PipeErr: (*tree)[i].PipeErr,
Expand All @@ -86,36 +86,38 @@ func compile(tree *Nodes, parent *proc.Process) {
}
}

func runNormal(tree *Nodes) (exitNum int) {
func runNormal(tree *astNodes) (exitNum int) {
if len(*tree) == 0 {
return 1
}

(*tree)[0].Process.Previous.Terminated = true
(*tree)[0].Process.Previous.HasTerminated = true

for i := range *tree {
if (*tree)[i].NewChain && i > 0 {
(*tree)[i-1].Process.Wait()
waitProcess(&(*tree)[i-1].Process)
}

go (*tree)[i].Process.Execute()
go executeProcess(&(*tree)[i].Process)
}

(*tree).Last().Process.Wait()
//(*tree).Last().Process.Wait()
waitProcess(&(*tree).Last().Process)
exitNum = (*tree).Last().Process.ExitNum
return
}

func runHyperSensitive(tree *Nodes) (exitNum int) {
func runHyperSensitive(tree *astNodes) (exitNum int) {
debug.Log("Entering Hyper Sensitive mode!!!")
if len(*tree) == 0 {
return 1
}

(*tree)[0].Process.Previous.Terminated = true
(*tree)[0].Process.Previous.HasTerminated = true

for i := range *tree {
(*tree)[i].Process.Execute()
//(*tree)[i].Process.Execute()
executeProcess(&(*tree)[i].Process)
exitNum = (*tree)[i].Process.ExitNum
if exitNum != 0 {
return
Expand Down
3 changes: 2 additions & 1 deletion lang/lang.go → lang/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

func ProcessNewBlock(block []rune, stdin, stdout, stderr streams.Io, gpName string) (exitNum int, err error) {
grandParent := new(proc.Process)
grandParent.OverrideProcName(gpName)
grandParent.Name = gpName
grandParent.MethodRef = gpName
grandParent.Parent = nil

if stdin != nil {
Expand Down
160 changes: 160 additions & 0 deletions lang/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package lang

import (
"encoding/json"
"fmt"
"github.com/lmorg/murex/debug"
"github.com/lmorg/murex/lang/proc/parameters"
"github.com/lmorg/murex/lang/proc/streams"
"github.com/lmorg/murex/lang/types"
"regexp"
)

var rxNewLine *regexp.Regexp = regexp.MustCompile(`[\r\n]+`)

func parseParameters(p *parameters.Parameters, vars *types.Vars) {
//p.Params = make([]string, 0)
//tCount := make([]bool, len(p.Tokens))
//var tCount []bool
debug.Json("####################################", p.Tokens)
for i := range p.Tokens {
p.Params = append(p.Params, "")
//tCount = append(tCount, false)
var tCount bool
for j := range p.Tokens[i] {
switch p.Tokens[i][j].Type {
case parameters.TokenTypeNil:
// do nothing

case parameters.TokenTypeValue:
p.Params[len(p.Params)-1] += p.Tokens[i][j].Key
tCount = true

case parameters.TokenTypeString:
p.Params[len(p.Params)-1] += vars.GetString(p.Tokens[i][j].Key)
tCount = true

case parameters.TokenTypeBlockString:
stdout := streams.NewStdin()
ProcessNewBlock([]rune(p.Tokens[i][j].Key), nil, stdout, nil, types.Null)
stdout.Close()
b := stdout.ReadAll()

if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}

if b[len(b)-1] == '\r' {
b = b[:len(b)-1]
}

p.Params[len(p.Params)-1] += string(b)
tCount = true

case parameters.TokenTypeArray:
var err error
var array []string

b := []byte(vars.GetString(p.Tokens[i][j].Key))

if types.IsArray(b) {
err = json.Unmarshal(b, &array)
}
if err != nil || !types.IsArray(b) {
array = rxNewLine.Split(string(b), -1)
}
if array[0] == "" {
array = array[1:]
}

if array[len(array)-1] == "" {
array = array[:len(array)-1]
}

if !tCount {
p.Params = p.Params[:len(p.Params)-1]
}

p.Params = append(p.Params, array...)
t := make([]bool, len(array))
for ti := range t {
t[ti] = true
}

tCount = true

case parameters.TokenTypeBlockArray:
var err error
var array []string

stdout := streams.NewStdin()
ProcessNewBlock([]rune(p.Tokens[i][j].Key), nil, stdout, nil, types.Null)
stdout.Close()

b := []byte(stdout.ReadAll())

if types.IsArray(b) {
err = json.Unmarshal(b, &array)
}
if err != nil || !types.IsArray(b) {
array = rxNewLine.Split(string(b), -1)
}
if array[0] == "" {
array = array[1:]
}

if array[len(array)-1] == "" {
array = array[:len(array)-1]
}

if !tCount {
p.Params = p.Params[:len(p.Params)-1]
}

p.Params = append(p.Params, array...)
t := make([]bool, len(array))
for ti := range t {
t[ti] = true
}

tCount = true

default:
panic(fmt.Sprintf(
`Unexpected parameter token type (%d) in parsed parameters. Param[%d][%d] == "%s"`,
p.Tokens[i][j].Type, i, j, p.Tokens[i][j].Key,
))
}
}
debug.Log("#######################################", tCount)
debug.Json("############### before ", p.Params)
if !tCount {
p.Params = p.Params[:len(p.Params)-1]
}
debug.Json("############### after ", p.Params)
}

/*if len(p.Tokens) != 0 && tCount[len(tCount)-1] == false {
if len(p.Tokens) == 1 {
p.Params = make([]string, 0)
} else {
p.Params = p.Params[:len(p.Tokens)-1]
}
}*/
/*for i := 0; i < len(tCount); i++ {
if !tCount[i] {
switch {
case i == 0:
p.Params = p.Params[1:]
tCount = tCount[1:]
case i == len(tCount):
p.Params = p.Params[:i]
tCount = tCount[:i]
default:
p.Params = append(p.Params[:i], p.Params[i+1:]...)
tCount = append(tCount[:i], tCount[i+1:]...)
}
i--
}
}*/
}
Loading