Beispiel #1
0
func TestParens(t *testing.T) {
	tests := []struct {
		str string
		ok  bool
	}{
		{"{}", true},
		{"{}{}", true},
		{"{{}}", true},
		{"{}}{", false},
		{"{{}}{}", true},
		{"}{", false},
	}
	for _, test := range tests {
		parser := parse.New()
		parens := &Parens{parser}
		parser.States["parens"] = parens
		parser.Push("parens")
		b := lex.Buffer{
			Taker: parser,
			Lex:   trie.New("trie", "{", "}"),
		}
		n, err := b.Read([]byte(test.str))
		if (n == len(test.str) && err == nil) != test.ok {
			if test.ok {
				t.Errorf("Expected no error on %s: %d read, error %s", test.str, n, err)
			} else {
				t.Errorf("Expected error on %s", test.str)
			}
		}
		fmt.Println("Done test!")
	}
}
Beispiel #2
0
func Translate(src string, context string) core.Program {
	tree, err := parse.New(context).Parse(src, "", "", make(map[string]*parse.Tree), builtins)

	if err != nil {
		fmt.Println(err)
	}

	prg := core.Program{}
	for _, node := range tree.Root.Nodes {
		prg = append(prg, translateNode(node))
	}
	return prg
}