codegenfw

package module
v0.0.0-...-e13150b Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: MIT Imports: 4 Imported by: 0

README

Code Generation Library (to generate C code)

Not usable yet......

package main

import "os"
import "io"
import "fmt"
import "github.com/byte-mug/codegenfw"

func out(w io.Writer) {
	blk := new(codegenfw.Block)
	blk.Childs.Init()
	{
		blk.Childs.PushBack(codegenfw.Declare("int","a"))
		blk.Childs.PushBack(codegenfw.NewLiteral("a","7"))
		blk.Childs.PushBack(codegenfw.TouchVariable("a"))
		blk.Childs.PushBack(codegenfw.NewLiteral(1,`"Let's begin!"`))
		blk.Childs.PushBack(codegenfw.NewCall("printf(%s)",nil,1))
		blk.Childs.PushBack(codegenfw.Label("restart"))
		doif := codegenfw.CS_If_Then_Else("a")
		// if-then-else
		blk.Childs.PushBack(doif)
			doif.Childs.PushBack(codegenfw.NewLiteral(1,`"Hello!"`))
			doif.Childs.PushBack(codegenfw.NewExpr("printf(%s)",0,nil,1))
		doelse := doif.EBlock
			doelse.Childs.PushBack(codegenfw.NewLiteral(2,`"Again!"`))
			doelse.Childs.PushBack(codegenfw.NewCall("printf(%s)",nil,2))
			doelse.Childs.PushBack(codegenfw.NewLiteral(3,`1`))
			doelse.Childs.PushBack(codegenfw.NewOp("(%s-%s)","a","a",3))
			doelse.Childs.PushBack(codegenfw.GoTo("restart"))
		
		/*
		Let's do:
		a = 1
		a = a+1
		a = a+1
		a = a+1
		a = a+1
		prinft("a = %d",a);
		*/
		
		blk.Childs.PushBack(codegenfw.NewLiteral("a","1")) // a = 1
		blk.Childs.PushBack(codegenfw.NewLiteral(4,`1`)) // $4 = 1
		blk.Childs.PushBack(codegenfw.NewLiteral(5,`"a = %d"`)) // $5 = "..."
		blk.Childs.PushBack(codegenfw.NewOp("(%s+%s)","a","a",4)) //a = a+$4
		blk.Childs.PushBack(codegenfw.NewOp("(%s+%s)","a","a",4)) //a = a+$4
		blk.Childs.PushBack(codegenfw.NewOp("(%s+%s)","a","a",4)) //a = a+$4
		blk.Childs.PushBack(codegenfw.NewOp("(%s+%s)","a","a",4)) //a = a+$4
		
		blk.Childs.PushBack(codegenfw.NewCall("printf(%s,%s)",nil,5,"a")) // printf(%5,a)
	}
	fmt.Fprintln(w,"#include","<stdio.h>")
	fmt.Fprintln(w)
	fmt.Fprintln(w,"void main(){")
	{
		gen := &codegenfw.Generator{Dest:w,Indent:"\t"}
		gen.Block(blk,codegenfw.GA_COUNT)
		gen.Block(blk,codegenfw.GA_GENERATE)
	}
	fmt.Fprintln(w,"}")
	fmt.Fprintln(w)
	fmt.Fprintln(w)
}

func main() {
	f,e := os.Create("program.c")
	if e!=nil { fmt.Println(e); return }
	defer f.Close()
	out(f)
}

Results in...

#include <stdio.h>

void main(){
	int a ;
	a = 7;
	printf("Let's begin!") ;
restart:
	if(a){
		printf("Hello!") ;
	}else{
		printf("Again!") ;
		a = (a-1) ;
		goto restart;
	}
	printf("a = %d",((((1+1)+1)+1)+1)) ;
}

Documentation

Index

Constants

View Source
const (
	GA_COUNT = GenAction(iota)
	GA_GENERATE
)
View Source
const (
	E_HAS_DEST = EFlags(1 << iota)
	E_CHEAP
	E_LITERAL
	E_NO_OMIT

	E_TIME_CRITICAL // time critical instructions must not be reordered.
)
View Source
const (
	GF_ENFORCE_STORE = GenFlags(1 << iota)
)

Variables

This section is empty.

Functions

func Decr

func Decr(i interface{}, ok bool) (interface{}, bool)

func GetNameChanger

func GetNameChanger(wf WordFilter) func(string) string

Creates a Name-Changing function, that replaces reserved identifiers (as specified trough 'wf') such as keywords with new names.

func Incr

func Incr(i interface{}, ok bool) (interface{}, bool)

func Noop

func Noop(i interface{}, ok bool) (interface{}, bool)

func Put

func Put(i interface{}) func(i interface{}, ok bool) (interface{}, bool)

func WF_ANSI_C

func WF_ANSI_C(s string) bool

func WF_GccC

func WF_GccC(s string) bool

This is a very Picky keyword filter, trying to cut off all GCC keywords.

func WF_ModernC

func WF_ModernC(s string) bool

WF_ANSI_C + asm typeof inline

Types

type Block

type Block struct {
	Childs list.List
}

type ControlStruct

type ControlStruct struct {
	Block
	Fmt  string
	Src  []ExprRef
	Fmt2 string
	Src2 []ExprRef

	// The "else"-block. This is called "else" block because
	// it is only useful when "if-then-else" is used.
	EBlock *Block
}

func CS_If_Then_Else

func CS_If_Then_Else(cond interface{}) *ControlStruct

func ControlStruct1

func ControlStruct1(fmt string, src ...interface{}) *ControlStruct

func ControlStruct2

func ControlStruct2(fmt string, src ...interface{}) *ControlStruct

func ControlStruct3

func ControlStruct3(fmt string, src []interface{}, fmt2 string, src2 []interface{}) *ControlStruct

type Declaration

type Declaration struct {
	DataType string
	Names    []string
}

func Declare

func Declare(t string, names ...string) *Declaration

type EFlags

type EFlags uint

func (EFlags) Has

func (e EFlags) Has(i EFlags) bool

type EnforceStore

type EnforceStore string

type Expr

type Expr struct {
	Dest  ExprRef
	Src   []ExprRef
	Fmt   string
	Flags EFlags
}

func NewCall

func NewCall(fmt string, dst interface{}, src ...interface{}) (result *Expr)

New Expression that is a call (eg. Flags = E_TIME_CRITICAL)

func NewExpr

func NewExpr(fmt string, f EFlags, dst interface{}, src ...interface{}) (result *Expr)

func NewLiteral

func NewLiteral(dst interface{}, val string) *Expr

func NewOp

func NewOp(fmt string, dst interface{}, src ...interface{}) (result *Expr)

New Operation (eg. Flags = E_CHEAP)

func NewSE

func NewSE(fmt string, dst interface{}, src ...interface{}) (result *Expr)

New Expression with side effect. (eg. Flags = E_NO_OMIT)

type ExprRef

type ExprRef struct {
	Name string
	Num  uint
}

func NewExprRef

func NewExprRef(i interface{}) ExprRef

func (ExprRef) SSA

func (e ExprRef) SSA() bool

type ExprRefMap

type ExprRefMap struct {
	// contains filtered or unexported fields
}

func (*ExprRefMap) Delete

func (e *ExprRefMap) Delete(k ExprRef)

func (*ExprRefMap) Iterate

func (e *ExprRefMap) Iterate(f func(k ExprRef, v interface{}))

func (*ExprRefMap) Update

func (e *ExprRefMap) Update(k ExprRef, f func(interface{}, bool) (interface{}, bool)) (interface{}, bool)

type GenAction

type GenAction uint

type GenFlags

type GenFlags uint

func (GenFlags) Has

func (a GenFlags) Has(b GenFlags) bool

type Generator

type Generator struct {
	Dest   io.Writer
	Indent string
	Flags  GenFlags
	// contains filtered or unexported fields
}

func (*Generator) Block

func (g *Generator) Block(b *Block, ga GenAction)

func (*Generator) Expr

func (g *Generator) Expr(e *Expr, ga GenAction)

func (*Generator) Sync

func (g *Generator) Sync(ga GenAction)

type GoTo

type GoTo string

type Label

type Label string

type SetVolatile

type SetVolatile struct {
	Variable string
	Volatile bool
}

type TouchVariable

type TouchVariable string

type WordFilter

type WordFilter func(s string) bool

Returns true if the word is reserved.

Jump to

Keyboard shortcuts

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