From 567142e0b629fe8ec98deb793efe2c0f4db97ec5 Mon Sep 17 00:00:00 2001 From: itsubaki <1759459+itsubaki@users.noreply.github.com> Date: Wed, 25 May 2022 23:28:03 +0900 Subject: [PATCH] Update some files --- pkg/ast/decl.go | 8 ------ pkg/ast/decl_test.go | 10 ------- pkg/ast/expr.go | 4 --- pkg/ast/expr_test.go | 10 ------- pkg/ast/stmt.go | 46 +++++++++++++++++++++++++++------ pkg/ast/stmt_test.go | 6 ++--- pkg/evaluator/evaluator.go | 42 ++++++++++++------------------ pkg/evaluator/evaluator_test.go | 3 +++ pkg/parser/parser.go | 14 +++++----- 9 files changed, 67 insertions(+), 76 deletions(-) diff --git a/pkg/ast/decl.go b/pkg/ast/decl.go index 7e82176..812b988 100644 --- a/pkg/ast/decl.go +++ b/pkg/ast/decl.go @@ -23,10 +23,6 @@ type DeclList struct { List []Decl } -func (d *DeclList) Append(e Decl) { - d.List = append(d.List, e) -} - func (d *DeclList) String() string { var list []string for _, e := range d.List { @@ -42,10 +38,6 @@ type ParenDecl struct { func (d *ParenDecl) declNode() {} -func (d *ParenDecl) Append(e Decl) { - d.List.Append(e) -} - func (d *ParenDecl) Literal() string { return lexer.Tokens[lexer.LPAREN] } diff --git a/pkg/ast/decl_test.go b/pkg/ast/decl_test.go index 2622f20..a2080c0 100644 --- a/pkg/ast/decl_test.go +++ b/pkg/ast/decl_test.go @@ -261,16 +261,6 @@ func TestDeclLiteral(t *testing.T) { } } -func TestDeclList(t *testing.T) { - l := &ast.DeclList{} - l.Append(&ast.BadDecl{}) - l.Append(&ast.BadDecl{}) - - if len(l.List) != 2 { - t.Errorf("invalid length=%v", len(l.List)) - } -} - func TestBadDecl(t *testing.T) { d := &ast.BadDecl{} diff --git a/pkg/ast/expr.go b/pkg/ast/expr.go index 2097b6f..e17dfec 100644 --- a/pkg/ast/expr.go +++ b/pkg/ast/expr.go @@ -30,10 +30,6 @@ func (x *ExprList) Literal() string { return "" } -func (x *ExprList) Append(e Expr) { - x.List = append(x.List, e) -} - func (x *ExprList) String() string { var list []string for _, e := range x.List { diff --git a/pkg/ast/expr_test.go b/pkg/ast/expr_test.go index 01e6257..443e6e7 100644 --- a/pkg/ast/expr_test.go +++ b/pkg/ast/expr_test.go @@ -345,16 +345,6 @@ func TestExprLiteral(t *testing.T) { } } -func TestExprList(t *testing.T) { - l := &ast.ExprList{} - l.Append(&ast.BadExpr{}) - l.Append(&ast.BadExpr{}) - - if len(l.List) != 2 { - t.Errorf("invalid length=%v", len(l.List)) - } -} - func TestBadExpr(t *testing.T) { x := &ast.BadExpr{} diff --git a/pkg/ast/stmt.go b/pkg/ast/stmt.go index 9262d9e..66aa0fa 100644 --- a/pkg/ast/stmt.go +++ b/pkg/ast/stmt.go @@ -251,17 +251,13 @@ func (s *BlockStmt) String() string { return buf.String() } -func (s *BlockStmt) Append(e Stmt) { - s.List = append(s.List, e) -} - -func (s *BlockStmt) Reverse() *BlockStmt { - var out BlockStmt +func (s *BlockStmt) Inv() *BlockStmt { + var rev BlockStmt for i := len(s.List) - 1; i > -1; i-- { - out.Append(s.List[i]) + rev.List = append(rev.List, s.List[i]) } - return &out + return rev.Add(Modifier{Kind: lexer.INV}) } func (s *BlockStmt) Pow(y int) *BlockStmt { @@ -273,6 +269,40 @@ func (s *BlockStmt) Pow(y int) *BlockStmt { return &out } +func (s *BlockStmt) Add(m Modifier) *BlockStmt { + var out BlockStmt + for _, b := range s.List { + switch s := b.(type) { + case *ApplyStmt: + out.List = append(out.List, &ApplyStmt{ + Kind: s.Kind, + Modifier: append(s.Modifier, m), + Name: s.Name, + Params: s.Params, + QArgs: s.QArgs, + }) + case *ExprStmt: + switch X := s.X.(type) { + case *CallExpr: + out.List = append(out.List, &ExprStmt{ + X: &CallExpr{ + Name: X.Name, + Modifier: append(X.Modifier, m), + Params: X.Params, + QArgs: X.QArgs, + }, + }) + default: + out.List = append(out.List, &ExprStmt{X: X}) + } + default: + out.List = append(out.List, s) + } + } + + return &out +} + type IfStmt struct{} type BranchStmt struct{} diff --git a/pkg/ast/stmt_test.go b/pkg/ast/stmt_test.go index 38a8396..912a04a 100644 --- a/pkg/ast/stmt_test.go +++ b/pkg/ast/stmt_test.go @@ -462,7 +462,7 @@ func TestStmt(t *testing.T) { } } -func ExampleBlockStmt_Reverse() { +func ExampleBlockStmt_Inv() { block := &ast.BlockStmt{ List: []ast.Stmt{ &ast.ApplyStmt{ @@ -494,14 +494,14 @@ func ExampleBlockStmt_Reverse() { }, } - rev := block.Reverse() + rev := block.Inv() fmt.Println(block) fmt.Println(rev) // Output: // { H q0; cx q0, q1; } - // { cx q0, q1; H q0; } + // { inv @ cx q0, q1; inv @ H q0; } } func TestBadStmt(t *testing.T) { diff --git a/pkg/evaluator/evaluator.go b/pkg/evaluator/evaluator.go index 11eabca..d9d8a43 100644 --- a/pkg/evaluator/evaluator.go +++ b/pkg/evaluator/evaluator.go @@ -150,9 +150,6 @@ func (e *Evaluator) eval(n ast.Node, env *env.Environ) (obj object.Object, err e c[i] = e[i].(*object.Int).Value } - case *ast.BlockStmt: - return e.EvalBolck(e.ModifyStmt(n, env), env) - case *ast.ReturnStmt: v, err := e.eval(n.Result, env) if err != nil { @@ -185,6 +182,9 @@ func (e *Evaluator) eval(n ast.Node, env *env.Environ) (obj object.Object, err e case *ast.CallExpr: return e.Call(n, env) + case *ast.BlockStmt: + return e.EvalBolck(e.ModifyStmt(n, env), env) + case *ast.MeasureExpr: return e.Measure(n, env) @@ -487,10 +487,10 @@ func (e *Evaluator) ApplyUAt(i int, mod []ast.Modifier, u matrix.Matrix, qargs [ } func (e *Evaluator) ApplyU(mod []ast.Modifier, u matrix.Matrix, qargs [][]q.Qubit, env *env.Environ) error { - // Modifier + // Modify inv, pow u = e.ModifyU(mod, u, env) - // ctrl + // Modify ctrl ctrl := ast.ModCtrl(mod) if len(ctrl) > 0 { // NOTE: That is, inv @ ctrl @ U = ctrl @ inv @ U. @@ -627,12 +627,17 @@ func (e *Evaluator) SetQArgs(env, outer *env.Environ, decl, args []ast.Expr) { } } - // fmt.Printf("decl: %v\n", decl) // fmt.Printf("args: %v\n", args) - // fmt.Printf("outer.Modifier: %v\n", outer.Modifier) - // fmt.Printf("outer.Qubit: %v\n", outer.Qubit) // fmt.Printf("env.Modifier: %v\n", env.Modifier) + // fmt.Printf("env.Decl: %v\n", env.Decl) + // fmt.Printf("out.Qubit: %v\n", outer.Qubit) // fmt.Printf("env.Qubit: %v\n", env.Qubit) + + // args: [c t] + // env.Modifier: [{51 {{[]}}}] + // env.Decl: [gate x q { U(pi, 0, pi) q; }] + // out.Qubit: [c t], map[c:[0] t:[1]] + // env.Qubit: [q], map[q:[0]] } // ModifyU returns modified(inv, pow) U @@ -650,7 +655,7 @@ func (e *Evaluator) ModifyU(mod []ast.Modifier, u matrix.Matrix, env *env.Enviro return u } -// ModifyStmt returns modified(inv, pow) BlockStmt +// ModifyStmt returns modified BlockStmt func (e *Evaluator) ModifyStmt(s *ast.BlockStmt, env *env.Environ) *ast.BlockStmt { // NOTE: pow(2) @ inv @ u is not equal to inv @ pow(2) @ u // @@ -682,25 +687,12 @@ func (e *Evaluator) ModifyStmt(s *ast.BlockStmt, env *env.Environ) *ast.BlockStm // inv @ pow(3) @ U(c, b, a) q; for _, m := range env.Modifier { switch m.Kind { + case lexer.CTRL, lexer.NEGCTRL: + s = s.Add(m) case lexer.INV: - s = s.Reverse() - - for _, b := range s.List { - switch s := b.(type) { - case *ast.ApplyStmt: - s.Modifier = append(s.Modifier, m) - case *ast.ExprStmt: - switch X := s.X.(type) { - case *ast.CallExpr: - X.Modifier = append(X.Modifier, m) - } - } - } - + s = s.Inv() case lexer.POW: s = s.Pow(int(ast.Must(e.eval(m.Index.List.List[0], env)).(*object.Int).Value)) - case lexer.CTRL, lexer.NEGCTRL: - // FIXME: copy s. add mod ctrl, negctrl } } diff --git a/pkg/evaluator/evaluator_test.go b/pkg/evaluator/evaluator_test.go index ba64e87..7862ba3 100644 --- a/pkg/evaluator/evaluator_test.go +++ b/pkg/evaluator/evaluator_test.go @@ -974,6 +974,9 @@ qubit c; qubit t; x c; +ctrl @ x c, t; +print; + ctrl @ x c, t; print; ` diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 64f90e9..c756de2 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -162,12 +162,11 @@ func (p *Parser) parseDeclStmt() ast.Stmt { func (p *Parser) parseDeclList() ast.DeclList { list := ast.DeclList{} - list.Append(p.parseDecl()) + list.List = append(list.List, p.parseDecl()) for p.next().Token == lexer.COMMA { p.next() // skip COMMA token - - list.Append(p.parseDecl()) + list.List = append(list.List, p.parseDecl()) } return list @@ -199,12 +198,11 @@ func (p *Parser) parseDecl() ast.Decl { func (p *Parser) parseIdentList() ast.ExprList { list := ast.ExprList{} - list.Append(p.parseIdent()) + list.List = append(list.List, p.parseIdent()) for p.next().Token == lexer.COMMA { p.next() // skip COMMNA token - - list.Append(p.parseIdent()) + list.List = append(list.List, p.parseIdent()) } return list @@ -362,7 +360,7 @@ func (p *Parser) parseGate() ast.Decl { p.expect(lexer.LBRACE) for p.next().Token != lexer.RBRACE { - decl.Body.Append(p.parseStmt()) + decl.Body.List = append(decl.Body.List, p.parseStmt()) } p.expect(lexer.RBRACE) @@ -420,7 +418,7 @@ func (p *Parser) parseFunc() ast.Decl { p.expect(lexer.LBRACE) for p.next().Token != lexer.RBRACE { - decl.Body.Append(p.parseStmt()) + decl.Body.List = append(decl.Body.List, p.parseStmt()) } p.expect(lexer.RBRACE)