gocc

package module
v0.0.0-...-6e10f1c Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 13 Imported by: 0

README

CI Build Status

gocc

a toy c compiler for macOS(m1) written in go.
macOS向け9ccのc実装(未完成)のgo再実装

参考:

条件分岐に使用するeq,neなどの一覧

memo:

  • Zero Flag (Z)
  • Carry Flag (C)
  • Negative Flag (N)
  • Overflow Flag (V)
ebnf
program    = toplevel*
toplevel   = ident "(" (ident ","?)* ")" stmt
stmt       = expr ";"
           | "return" expr ";"
           | "if" "(" expr ")" stmt ("else" stmt)?
           | "while "(" expr ")" stmt
           | "for" "(" expr? ";" expr? ";" expr? ")" stmt
           | "{" stmt* "}"
expr       = assign
assign     = andor ("=" assign)?
andor      = equality ("&&" equality | "||" equality)*
equality   = relational ("==" relational | "!=" relational)*
relational = add ("<" add | "<=" add | ">" add | ">=" add)*
add        = mul ("+" mul | "-" mul)*
mul        = unary ("*" unary | "/" unary)*
unary      = ("+" | "-")? primary
primary    = num
           | ident ("(" (expr ","?)* ")")?
           | "(" expr ")"
           | """ ascii* """
ascii      = (0x21 - 0x7E)

Documentation

Index

Constants

View Source
const (
	SourceCode = iota
	Comment
	Separator
)
View Source
const (
	NdADD = iota // +
	NdSUB        // -
	NdMUL        // *
	NdDIV        // /

	NdEQ  // ==
	NdNE  //!=
	NdLT  // <
	NdLE  // <=
	NdAND // &&
	NdOR  // ||

	NdASSIGN // =

	NdNUM  // 数字
	NdLVAR // Local VAR
	NdCALL

	NdRETURN
	NdIF     //
	NdIFELSE //
	NdWHILE
	NdFOR

	NdBLOCK // "{" stmt* "}"
	NdFUNCTION
)
View Source
const (
	TkRESERVED = iota
	TkNUM
	TkIDENT
	TkRETURN
	TkIF
	TkELSE
	TkWHILE
	TkFOR
	TkEOF
)

Variables

View Source
var Assembly []*Line

Functions

func ExecAndGetExitCode

func ExecAndGetExitCode(compilerPath string, in string) int

func Gen

func Gen(node *Node)

func GenFizzBuzz

func GenFizzBuzz(in int) string

GenFizzBuzz return 15x: 0, 3x: 1, 5x: 2, else: 3

func GenStmt

func GenStmt() (int, [2]string, map[string]interface{})

func Generate

func Generate(nodes []*Node) string

Types

type DefString

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

func NewDefString

func NewDefString(label, data string) *DefString

type Line

type Line struct {
	Kind           LineKind
	Body           string
	Comment        string
	ExtendNL       int
	BlockId        string
	BlockSeparator string
	Nest           int
	Data           string
}

func NewComment

func NewComment(comment string) *Line

func NewSeparator

func NewSeparator(blockId, separator string, nest int, nl bool) *Line

func NewSrcLine

func NewSrcLine(body, comment string) *Line

func (*Line) String

func (l *Line) String() string

type LineKind

type LineKind int

type LocalVariable

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

type Node

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

func NewNode

func NewNode(kind NodeKind, lhs *Node, rhs *Node) *Node

func NewNodeBlock

func NewNodeBlock(code []*Node) *Node

func NewNodeCALL

func NewNodeCALL(functionName []rune, args []*Node) *Node

func NewNodeFunction

func NewNodeFunction(name []rune, args []*Node, block *Node) *Node

func NewNodeLVar

func NewNodeLVar(offset int) *Node

func NewNodeNum

func NewNodeNum(val int) *Node

func NewNodeWithExpr

func NewNodeWithExpr(Kind NodeKind, init, cond, loop, lhs, rhs *Node) *Node

func Parse

func Parse(tok *Token) []*Node

type NodeKind

type NodeKind int

func (NodeKind) String

func (n NodeKind) String() string

type Token

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

func NewToken

func NewToken(kind TokenKind, cur *Token, str []rune, len int) *Token

func Tokenize

func Tokenize(r []rune) *Token

type TokenKind

type TokenKind int

func (TokenKind) String

func (tk TokenKind) String() string

Directories

Path Synopsis
cmd
gocc command
gocc_old command

Jump to

Keyboard shortcuts

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