gen

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package gen provides templated output generation from addition chain programs.

Index

Constants

This section is empty.

Variables

View Source
var Functions = []*Function{
	{
		Name:        "add",
		Description: "If the input operation is an `ir.Add` then return it, otherwise return `nil`",
		Func: func(op ir.Op) ir.Op {
			if a, ok := op.(ir.Add); ok {
				return a
			}
			return nil
		},
	},
	{
		Name:        "double",
		Description: "If the input operation is an `ir.Double` then return it, otherwise return `nil`",
		Func: func(op ir.Op) ir.Op {
			if d, ok := op.(ir.Double); ok {
				return d
			}
			return nil
		},
	},
	{
		Name:        "shift",
		Description: "If the input operation is an `ir.Shift` then return it, otherwise return `nil`",
		Func: func(op ir.Op) ir.Op {
			if s, ok := op.(ir.Shift); ok {
				return s
			}
			return nil
		},
	},
	{
		Name:        "inc",
		Description: "Increment an integer",
		Func:        func(n int) int { return n + 1 },
	},
	{
		Name:        "format",
		Description: "Formats an addition chain script (`*ast.Chain`) as a string",
		Func:        printer.String,
	},
	{
		Name:        "split",
		Description: "Calls `strings.Split`",
		Func:        strings.Split,
	},
	{
		Name:        "join",
		Description: "Calls `strings.Join`",
		Func:        strings.Join,
	},
	{
		Name:        "lines",
		Description: "Split input string into lines",
		Func: func(s string) []string {
			var lines []string
			scanner := bufio.NewScanner(strings.NewReader(s))
			for scanner.Scan() {
				lines = append(lines, scanner.Text())
			}
			return lines
		},
	},
}

Functions is the list of functions provided to templates.

Functions

func BuiltinTemplate

func BuiltinTemplate(name string) (string, error)

BuiltinTemplate loads the named template. Returns an error if the template is unknown.

func BuiltinTemplateNames

func BuiltinTemplateNames() []string

BuiltinTemplateNames returns all builtin template names.

func Generate

func Generate(w io.Writer, tmpl string, d *Data) error

Generate templated output for the given data, writing to w.

func IsBuiltinTemplate

func IsBuiltinTemplate(name string) bool

IsBuiltinTemplate reports whether name is a builtin template name.

Types

type Config

type Config struct {
	// Allocator for temporary variables. This configuration determines variable
	// naming.
	Allocator pass.Allocator
}

Config for template input generation.

type Data

type Data struct {
	// Chain is the addition chain as a list of integers.
	Chain addchain.Chain

	// Ops is the complete sequence of addition operations required to compute
	// the addition chain.
	Ops addchain.Program

	// Script is the condensed representation of the addition chain computation
	// in the "addition chain calculator" language.
	Script *ast.Chain

	// Program is the intermediate representation of the addition chain
	// computation. This representation is likely the most convenient for code
	// generation. It contains a sequence of add, double and shift (repeated
	// doubling) instructions required to compute the chain. Temporary variable
	// allocation has been performed and the list of required temporaries
	// populated.
	Program *ir.Program

	// Metadata about the addchain project and the specific release parameters.
	// Please use this to include a reference or citation back to the addchain
	// project in your generated output.
	Meta *meta.Properties
}

Data provided to templates.

func PrepareData

func PrepareData(cfg Config, s *ast.Chain) (*Data, error)

PrepareData builds input template data for the given addition chain script.

type Function

type Function struct {
	Name        string
	Description string
	Func        interface{}
}

Function is a function provided to templates.

func (*Function) Signature

func (f *Function) Signature() string

Signature returns the function signature.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL