Skip to content

Commit

Permalink
Rename FuncDecl to SubroutineDecl
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Aug 17, 2023
1 parent b30c212 commit 1e4f493
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Ident(x interface{}) (string, error) {
return x.Name, nil // const N = 15
case *GateDecl:
return x.Name, nil // gate X {}
case *FuncDecl:
case *SubroutineDecl:
return x.Name, nil // def shor(){}
case *BasicLit:
return x.Value, nil
Expand Down
2 changes: 1 addition & 1 deletion ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestIdent(t *testing.T) {
{&ast.GenDecl{Name: "gendecl"}, "gendecl", false},
{&ast.GenConst{Name: "genconst"}, "genconst", false},
{&ast.GateDecl{Name: "gatedecl"}, "gatedecl", false},
{&ast.FuncDecl{Name: "funcdecl"}, "funcdecl", false},
{&ast.SubroutineDecl{Name: "subroutinedecl"}, "subroutinedecl", false},
{&ast.BasicLit{Value: "basic"}, "basic", false},
{"foobar", "", true},
}
Expand Down
8 changes: 4 additions & 4 deletions ast/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ func (d *GateDecl) String() string {
return buf.String()
}

type FuncDecl struct {
type SubroutineDecl struct {
Name string
Params ParenDecl
QArgs DeclList
Body BlockStmt
Result Expr
}

func (d *FuncDecl) declNode() {}
func (d *SubroutineDecl) declNode() {}

func (d *FuncDecl) Literal() string {
func (d *SubroutineDecl) Literal() string {
return lexer.Tokens[lexer.DEF]
}

func (d *FuncDecl) String() string {
func (d *SubroutineDecl) String() string {
var buf bytes.Buffer

buf.WriteString(d.Literal())
Expand Down
2 changes: 1 addition & 1 deletion ast/decl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestDecl(t *testing.T) {
"(int[32] a, int[32] N)",
},
{
&ast.FuncDecl{
&ast.SubroutineDecl{
Name: "shor",
Params: ast.ParenDecl{
List: ast.DeclList{
Expand Down
2 changes: 1 addition & 1 deletion ast/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *DeclStmt) Literal() string {

func (s *DeclStmt) String() string {
switch d := s.Decl.(type) {
case *GateDecl, *FuncDecl:
case *GateDecl, *SubroutineDecl:
return d.String()
}

Expand Down
41 changes: 24 additions & 17 deletions evaluator/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ import (
)

type Environ struct {
Outer *Environ
Bit *Bit
Qubit *Qubit
Const Const
Gate Gate
Modifier []ast.Modifier
Decl []ast.Decl
CtrlQArgs []ast.Expr
Outer *Environ
Bit *Bit
Qubit *Qubit
Const Const
Gate Gate
Subroutine Subroutine
Modifier []ast.Modifier
Decl []ast.Decl
CtrlQArgs []ast.Expr
}

// NewEnviron returns a new environment.
func NewEnviron() *Environ {
return &Environ{
Outer: nil,
Bit: NewBit(),
Qubit: NewQubit(),
Gate: make(map[string]ast.Decl),
Const: make(map[string]object.Object),
Modifier: make([]ast.Modifier, 0),
Decl: make([]ast.Decl, 0),
CtrlQArgs: nil,
Outer: nil,
Bit: NewBit(),
Qubit: NewQubit(),
Const: make(map[string]object.Object),
Gate: make(map[string]ast.Decl),
Subroutine: make(map[string]ast.Decl),
Modifier: make([]ast.Modifier, 0),
Decl: make([]ast.Decl, 0),
CtrlQArgs: nil,
}
}

Expand All @@ -47,11 +49,16 @@ func (e *Environ) NewEnclosed(decl ast.Decl, mod []ast.Modifier) *Environ {
}

func (e *Environ) String() string {
return fmt.Sprintf("gate: %v, const: %v, bit: %v, qubit: %v, modifier: %v, decl: %v", e.Gate, e.Const, e.Bit, e.Qubit, e.Modifier, e.Decl)
return fmt.Sprintf(
"bit: %v, qubit: %v, const: %v, gate: %v, subroutine: %v, modifier: %v, decl: %v",
e.Bit, e.Qubit, e.Const, e.Gate, e.Subroutine, e.Modifier, e.Decl,
)
}

type Gate map[string]ast.Decl

type Subroutine map[string]ast.Decl

type Const map[string]object.Object

type Bit struct {
Expand Down
2 changes: 1 addition & 1 deletion evaluator/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func ExampleEnviron() {
fmt.Println(env)

// Output:
// gate: map[], const: map[], bit: [], map[], qubit: [], map[], modifier: [], decl: []
// bit: [], map[], qubit: [], map[], const: map[], gate: map[], subroutine: map[], modifier: [], decl: []
}
6 changes: 3 additions & 3 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (e *Evaluator) eval(n ast.Node, env *Environ) (obj object.Object, err error
env.Gate[ast.Must(ast.Ident(n))] = n
return &object.Nil{}, nil

case *ast.FuncDecl:
env.Gate[ast.Must(ast.Ident(n))] = n
case *ast.SubroutineDecl:
env.Subroutine[ast.Must(ast.Ident(n))] = n
return &object.Nil{}, nil

case *ast.IdentExpr:
Expand Down Expand Up @@ -684,7 +684,7 @@ func (e *Evaluator) Call(x *ast.CallExpr, outer *Environ) (object.Object, error)
switch decl := f.(type) {
case *ast.GateDecl:
return e.eval(&decl.Body, e.Enclosed(x, decl, outer))
case *ast.FuncDecl:
case *ast.SubroutineDecl:
return nil, fmt.Errorf("not implemented=%v", f)
}

Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (p *Parser) parseFunc() ast.Decl {
p.expect(lexer.IDENT)

// def shor
decl := ast.FuncDecl{
decl := ast.SubroutineDecl{
Name: ident.Literal,
Body: ast.BlockStmt{},
}
Expand Down

0 comments on commit 1e4f493

Please sign in to comment.