package terse.vm;

import terse.vm.Expr.Top;
import terse.vm.Parser.TLex;
import terse.vm.Terp;
import junit.framework.TestCase;

public class ParserTest extends TestCase {
	Terp t;

	protected void setUp() throws Exception {
		super.setUp();
		t = new Terp.PosixTerp(false, "" /* no name for tests */);
	}

	public void testTLex() {
		TLex lex = new TLex(t, "  88.5 ");
		assertEquals(Parser.Pat.NUMBER, lex.t);
		assertEquals("88.5", lex.w);
	}

	public void testTuple() {
		Cls cls = t.tTmp;
		Top a = Parser.parseMethod(cls, "bogus", " 111, 222, 333 ");
		assertEquals(" (111, 222, 333, ) ", a.body.toString());
	}

	public void testTupleList() {
		Cls cls = t.tTmp;
		Top a = Parser.parseMethod(cls, "bogus", " 111, 222; 333, 444; ");
		assertEquals(" ( (111, 222, ) ;  (333, 444, ) ; ) ", a.body.toString());
	}

	public void testSimpleBinop() {
		Cls cls = t.tTmp;
		Top a = Parser.parseMethod(cls, "bogus", " 111 * 2, 222 + 3, 333 > 4");
		assertEquals(" (111 * 2 , 222 + 3 , 333 > 4 , ) ", a.body.toString());
	}
	public void testLayeredBinop() {
		Cls cls = t.tTmp;
		Top a = Parser.parseMethod(cls, "bogus", "0 at: 1 != 2 + 3 * 4");
		assertEquals("0 at: (1 != (2 + (3 * 4 ) ) ) ", a.body.toString());
	}
	public void testLayeredBinopMore() {
		Cls cls = t.tTmp;
		Top a = Parser.parseMethod(cls, "bogus", "0 at: 1 != 2 + 3 * 4 * 5 + 6 == 7");
		assertEquals("0 at: ((1 != ((2 + ((3 * 4 ) * 5 ) ) + 6 ) ) == 7 ) ", a.body.toString());
	}	
	public void testTrigraphs() {
		assertEquals("a<b>c", Parser.trigraphSubst("a/ltb/gtc"));
	}
	public void testBigraphs() {
		assertEquals("a:b;c;d$e=f(g)h", Parser.bigraphSubst("a..b,.c.,d,,e--f!g?h"));
	}
	public void testBigraphVsNotEqual() {
		assertEquals("a:b;c;d$e=f!=g", Parser.bigraphSubst("a..b,.c.,d,,e--f!=g"));
	}
	public void testAssignTuple() {
		assertEquals(t.newNum(25), t.newTmp().eval("x, y = Vec(3; 4;).   x * x + y * y."));
	}
}


