コード例 #1
0
ファイル: parser_test.go プロジェクト: danwakefield/gosh
func TestParserAssignment(t *testing.T) {
	type TestCase struct {
		inString string
		inVars   map[string]string
		want     int64
		wantVars map[string]string
	}
	cases := []TestCase{
		{
			"x=2",
			map[string]string{},
			2,
			map[string]string{"x": "2"},
		},
		{
			"x+=2",
			map[string]string{},
			2,
			map[string]string{"x": "2"},
		},
		{
			"x+=2",
			map[string]string{"x": "2"},
			4,
			map[string]string{"x": "4"},
		},
		{
			"x*=4",
			map[string]string{"x": "2"},
			8,
			map[string]string{"x": "8"},
		},
		{
			"0 ? x += 2 : x += 2",
			map[string]string{},
			2,
			map[string]string{"x": "2"},
		},
	}

	for _, c := range cases {
		scp := variables.NewScope()
		for k, v := range c.inVars {
			scp.Set(k, v)
		}
		got, err := A.Parse(c.inString, scp)
		if err != nil {
			t.Errorf("Parse returned an error: %s", err.Error())
		}
		if got != c.want {
			t.Errorf("Variable assignment '%s' should evaluate to '%d'", c.inString, c.want)
		}
		for varName, wantVar := range c.wantVars {
			gotVar := scp.Get(varName)
			if gotVar.Val != wantVar {
				t.Errorf("Variable assignment should modify global scope. '%s' should be '%s' not '%s'", varName, wantVar, gotVar.Val)
			}
		}
	}
}
コード例 #2
0
ファイル: in.go プロジェクト: danwakefield/gosh
func main() {
	if len(os.Args) < 2 {
		fmt.Println("Please pass a file to run")
		os.Exit(1)
	}
	fileContents, err := ioutil.ReadFile(os.Args[1]) // Ignore error
	if err != nil {
		fmt.Println(err.Error())
	}
	p := NewParser(string(fileContents))
	scp := variables.NewScope()
	scp.SetPositionalArgs(os.Args[1:])
	ex := T.ExitSuccess

	stdIO := &T.IOContainer{In: os.Stdin, Out: os.Stdout, Err: os.Stderr}

	for {
		n := p.Parse()
		if n == nil {
			//Newline
			continue
		}
		if _, isEOF := n.(NodeEOF); isEOF {
			os.Exit(int(ex))
		}

		ex = n.Eval(scp, stdIO)
	}
}
コード例 #3
0
ファイル: parser_test.go プロジェクト: danwakefield/gosh
package arith_test

import (
	"testing"

	A "github.com/danwakefield/gosh/arith"
	"github.com/danwakefield/gosh/variables"
)

var EmptyScope = variables.NewScope()

func TestParserBinops(t *testing.T) {
	type TestCase struct {
		in   string
		want int64
	}
	cases := []TestCase{
		{"5 <= 4", A.ShellFalse},
		{"4 <= 4", A.ShellTrue},
		{"3 <= 4", A.ShellTrue},
		{"3 >= 4", A.ShellFalse},
		{"4 >= 4", A.ShellTrue},
		{"5 >= 4", A.ShellTrue},
		{"5 <  4", A.ShellFalse},
		{"3 <  4", A.ShellTrue},
		{"3 >  4", A.ShellFalse},
		{"5 >  4", A.ShellTrue},
		{"5 == 4", A.ShellFalse},
		{"4 == 4", A.ShellTrue},
		{"4 != 4", A.ShellFalse},
		{"5 != 4", A.ShellTrue},