Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler] Support inherited functions and conditions through delegation #3734

Merged
merged 19 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
473 changes: 179 additions & 294 deletions bbq/compiler/compiler.go

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions bbq/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCompileRecursionFib(t *testing.T) {
`)
require.NoError(t, err)

compiler := NewBytecodeCompiler(checker.Program, checker.Elaboration)
compiler := NewBytecodeCompiler(checker)
program := compiler.Compile()

require.Len(t, program.Functions, 1)
Expand Down Expand Up @@ -71,8 +71,12 @@ func TestCompileRecursionFib(t *testing.T) {
byte(opcode.Transfer), 0, 0,
byte(opcode.GetGlobal), 0, 0,
byte(opcode.Invoke), 0, 0,
// return sum
byte(opcode.IntAdd),
// assign to temp $result
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 1,
// return $result
byte(opcode.GetLocal), 0, 1,
byte(opcode.ReturnValue),
},
compiler.ExportFunctions()[0].Code,
Expand Down Expand Up @@ -114,7 +118,7 @@ func TestCompileImperativeFib(t *testing.T) {
`)
require.NoError(t, err)

compiler := NewBytecodeCompiler(checker.Program, checker.Elaboration)
compiler := NewBytecodeCompiler(checker)
program := compiler.Compile()

require.Len(t, program.Functions, 1)
Expand Down Expand Up @@ -163,8 +167,12 @@ func TestCompileImperativeFib(t *testing.T) {
byte(opcode.SetLocal), 0, 4,
// continue loop
byte(opcode.Jump), 0, 36,
// return fibonacci
// assign to temp $result
byte(opcode.GetLocal), 0, 3,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 5,
// return $result
byte(opcode.GetLocal), 0, 5,
byte(opcode.ReturnValue),
},
compiler.ExportFunctions()[0].Code,
Expand Down Expand Up @@ -203,7 +211,7 @@ func TestCompileBreak(t *testing.T) {
`)
require.NoError(t, err)

compiler := NewBytecodeCompiler(checker.Program, checker.Elaboration)
compiler := NewBytecodeCompiler(checker)
program := compiler.Compile()

require.Len(t, program.Functions, 1)
Expand Down Expand Up @@ -231,8 +239,12 @@ func TestCompileBreak(t *testing.T) {
byte(opcode.SetLocal), 0, 0,
// repeat
byte(opcode.Jump), 0, 9,
// return i
// assign i to temp $result
byte(opcode.GetLocal), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 1,
// return $result
byte(opcode.GetLocal), 0, 1,
byte(opcode.ReturnValue),
},
compiler.ExportFunctions()[0].Code,
Expand Down Expand Up @@ -276,7 +288,7 @@ func TestCompileContinue(t *testing.T) {
`)
require.NoError(t, err)

compiler := NewBytecodeCompiler(checker.Program, checker.Elaboration)
compiler := NewBytecodeCompiler(checker)
program := compiler.Compile()

require.Len(t, program.Functions, 1)
Expand Down Expand Up @@ -306,8 +318,12 @@ func TestCompileContinue(t *testing.T) {
byte(opcode.Jump), 0, 45,
// repeat
byte(opcode.Jump), 0, 9,
// return i
// assign i to temp $result
byte(opcode.GetLocal), 0, 0,
byte(opcode.Transfer), 0, 0,
byte(opcode.SetLocal), 0, 1,
// return $result
byte(opcode.GetLocal), 0, 1,
byte(opcode.ReturnValue),
},
compiler.ExportFunctions()[0].Code,
Expand Down
9 changes: 6 additions & 3 deletions bbq/compiler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ package compiler

import (
"github.com/onflow/cadence/bbq/commons"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/sema"
)

type Config[E any] struct {
ImportHandler commons.ImportHandler
LocationHandler commons.LocationHandler
type Config struct {
ImportHandler commons.ImportHandler
LocationHandler commons.LocationHandler
ElaborationResolver func(common.Location) (*sema.Elaboration, error)
}
Loading
Loading