tinyphp

package module
v0.0.0-...-36d1133 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2020 License: MIT Imports: 9 Imported by: 5

README

High performance PHP runtime support basic syntax and functions.

godoc goreport codecov license


Thanks to anko for the lexer (most codes copied from anko) and rules of goyacc.


Try

make cli
./tinyphp scripts/hello.php

Click here to play online.

Embed

package main

import (
	"github.com/kimkit/tinyphp"
	"github.com/kimkit/tinyphp/std"
)

func main() {
	src := `
var_dump("hello world");
    `
	stmts, err := tinyphp.ParseSrc(src)
	if err != nil {
		panic(err)
	}
	store := tinyphp.NewStore(std.Funcs, std.Methods)
	if _, err := tinyphp.RunStmts(stmts, store); err != nil {
		panic(err)
	}
}

Architecture

Ecosystem

Documentation

Index

Constants

View Source
const (
	EOF = -1
	EOL = '\n'
)
View Source
const (
	ADD    = int('+')
	SUB    = int('-')
	MUL    = int('*')
	DIV    = int('/')
	MOD    = int('%')
	AND    = int('&')
	OR     = int('|')
	XOR    = int('^')
	NOT    = int('!')
	GT     = int('>')
	LT     = int('<')
	CONCAT = int('.')
	REV    = int('~')
)
View Source
const (
	TypeUnknown int = iota
	TypeInt
	TypeFloat
	TypeString
	TypeBool
	TypeNull
)
View Source
const ADDADD = 57365
View Source
const ADDEQ = 57384
View Source
const ANDAND = 57373
View Source
const ANDEQ = 57390
View Source
const ARROW = 57359
View Source
const AS = 57358
View Source
const BREAK = 57360
View Source
const CONCATEQ = 57383
View Source
const CONTINUE = 57361
View Source
const DIVEQ = 57387
View Source
const ECHO = 57375
View Source
const ELSE = 57355
View Source
const EQEQ = 57369
View Source
const EQEQEQ = 57377
View Source
const FALSE = 57350
View Source
const FLOAT = 57347
View Source
const FOR = 57356
View Source
const FOREACH = 57357
View Source
const FUNCNAME = 57364
View Source
const FUNCTION = 57362
View Source
const GTEQ = 57370
View Source
const IDENT = 57352
View Source
const IF = 57354
View Source
const INT = 57346
View Source
const ISSET = 57376
View Source
const LEFT = 57367
View Source
const LEFTEQ = 57393
View Source
const LTEQ = 57371
View Source
const LTEQGT = 57379
View Source
const METHOD = 57353
View Source
const MODEQ = 57388
View Source
const MULEQ = 57386
View Source
const NEQ = 57372
View Source
const NEQEQ = 57378
View Source
const NULL = 57351
View Source
const NULLEQ = 57380
View Source
const OREQ = 57391
View Source
const OROR = 57374
View Source
const POW = 57382
View Source
const POWEQ = 57389
View Source
const RETURN = 57363
View Source
const RIGHT = 57368
View Source
const RIGHTEQ = 57394
View Source
const STRING = 57348
View Source
const SUBEQ = 57385
View Source
const SUBSUB = 57366
View Source
const TRUE = 57349
View Source
const TRUEEQ = 57381
View Source
const UNARY = 57395
View Source
const XOREQ = 57392

Variables

Functions

func DefaultOutputHandler

func DefaultOutputHandler(store *Store, v ...interface{})

func DumpExprs

func DumpExprs(exprs []Expr, prefix string) string

func DumpStmts

func DumpStmts(stmts []Stmt, prefix string) string

func EnableDebug

func EnableDebug(level int)

func EnableErrorVerbose

func EnableErrorVerbose()

func GetPool

func GetPool(size int) *sync.Pool

func GetTypeName

func GetTypeName(val interface{}) string

func InitStmts

func InitStmts(stmts []Stmt, w *Walker) error

func IsBool

func IsBool(val interface{}) bool

func IsFloat

func IsFloat(val interface{}) bool

func IsInt

func IsInt(val interface{}) bool

func IsNull

func IsNull(val interface{}) bool

func IsString

func IsString(val interface{}) bool

func RunSrc

func RunSrc(src string, store *Store) (interface{}, error)

func RunStmts

func RunStmts(stmts []Stmt, store *Store) (interface{}, error)

func ToBool

func ToBool(val interface{}) bool

func ToFloat

func ToFloat(val interface{}) float64

func ToInt

func ToInt(val interface{}) int

func ToString

func ToString(val interface{}) string

Types

type BreakStmt

type BreakStmt struct {
	StmtImpl
}

func (*BreakStmt) Dump

func (stmt *BreakStmt) Dump(prefix string) string

func (*BreakStmt) Init

func (stmt *BreakStmt) Init(w *Walker) error

func (*BreakStmt) Run

func (stmt *BreakStmt) Run(store *Store) (interface{}, error)

type CalcExpr

type CalcExpr struct {
	ExprImpl
	LeftExpr  Expr // nil
	RightExpr Expr
	Op        int
}

func (*CalcExpr) Dump

func (expr *CalcExpr) Dump(prefix string) string

func (*CalcExpr) Init

func (expr *CalcExpr) Init(w *Walker) error

func (*CalcExpr) Run

func (expr *CalcExpr) Run(store *Store) (interface{}, error)

type CondExpr

type CondExpr struct {
	ExprImpl
	Expr     Expr
	IfExpr   Expr
	ElseExpr Expr
}

func (*CondExpr) Dump

func (expr *CondExpr) Dump(prefix string) string

func (*CondExpr) Init

func (expr *CondExpr) Init(w *Walker) error

func (*CondExpr) Run

func (expr *CondExpr) Run(store *Store) (interface{}, error)

type ContinueStmt

type ContinueStmt struct {
	StmtImpl
}

func (*ContinueStmt) Dump

func (stmt *ContinueStmt) Dump(prefix string) string

func (*ContinueStmt) Init

func (stmt *ContinueStmt) Init(w *Walker) error

func (*ContinueStmt) Run

func (stmt *ContinueStmt) Run(store *Store) (interface{}, error)

type Dump

type Dump interface {
	Dump(prefix string) string
}

type EchoStmt

type EchoStmt struct {
	StmtImpl
	Expr Expr
}

func (*EchoStmt) Dump

func (stmt *EchoStmt) Dump(prefix string) string

func (*EchoStmt) Init

func (stmt *EchoStmt) Init(w *Walker) error

func (*EchoStmt) Run

func (stmt *EchoStmt) Run(store *Store) (interface{}, error)

type Expr

type Expr interface {
	Pos
	Run
	Init
	Dump
}

type ExprImpl

type ExprImpl struct {
	PosImpl
}

type ExprStmt

type ExprStmt struct {
	StmtImpl
	Expr Expr
}

func (*ExprStmt) Dump

func (stmt *ExprStmt) Dump(prefix string) string

func (*ExprStmt) Init

func (stmt *ExprStmt) Init(w *Walker) error

func (*ExprStmt) Run

func (stmt *ExprStmt) Run(store *Store) (interface{}, error)

type ForStmt

type ForStmt struct {
	StmtImpl
	InitExpr Expr // nil
	CompExpr Expr // nil
	LoopExpr Expr // nil
	Stmts    []Stmt
}

func (*ForStmt) Dump

func (stmt *ForStmt) Dump(prefix string) string

func (*ForStmt) Init

func (stmt *ForStmt) Init(w *Walker) error

func (*ForStmt) Run

func (stmt *ForStmt) Run(store *Store) (interface{}, error)

type ForeachStmt

type ForeachStmt struct {
	StmtImpl
	Expr       Expr
	IndexKey   int
	IdentKey   string
	IndexValue int
	IdentValue string
	Stmts      []Stmt
}

func (*ForeachStmt) Dump

func (stmt *ForeachStmt) Dump(prefix string) string

func (*ForeachStmt) Init

func (stmt *ForeachStmt) Init(w *Walker) error

func (*ForeachStmt) Run

func (stmt *ForeachStmt) Run(store *Store) (interface{}, error)

type ForkHandler

type ForkHandler func() *Store

type Func

type Func func(*Store, []interface{}) (interface{}, error)

type FuncExpr

type FuncExpr struct {
	ExprImpl
	Index int
	Name  string
	Args  []Expr
}

func (*FuncExpr) Dump

func (expr *FuncExpr) Dump(prefix string) string

func (*FuncExpr) Init

func (expr *FuncExpr) Init(w *Walker) error

func (*FuncExpr) Run

func (expr *FuncExpr) Run(store *Store) (interface{}, error)

type FuncStmt

type FuncStmt struct {
	StmtImpl
	Index  int
	Name   string // ""
	Indexs []int
	Idents []string
	Stmts  []Stmt
}

func (*FuncStmt) Dump

func (stmt *FuncStmt) Dump(prefix string) string

func (*FuncStmt) Init

func (stmt *FuncStmt) Init(w *Walker) error

func (*FuncStmt) Run

func (stmt *FuncStmt) Run(store *Store) (interface{}, error)

type IdentExpr

type IdentExpr struct {
	ExprImpl
	Index int
	Ident string // ""
	Expr  Expr   // nil
}

func (*IdentExpr) Dump

func (expr *IdentExpr) Dump(prefix string) string

func (*IdentExpr) Init

func (expr *IdentExpr) Init(w *Walker) error

func (*IdentExpr) Run

func (expr *IdentExpr) Run(store *Store) (interface{}, error)

type IfStmt

type IfStmt struct {
	StmtImpl
	Expr        Expr
	Stmts       []Stmt
	ElseIfExpr  []Expr
	ElseIfStmts [][]Stmt
	ElseStmts   []Stmt
}

func (*IfStmt) Dump

func (stmt *IfStmt) Dump(prefix string) string

func (*IfStmt) Init

func (stmt *IfStmt) Init(w *Walker) error

func (*IfStmt) Run

func (stmt *IfStmt) Run(store *Store) (interface{}, error)

type IncrExpr

type IncrExpr struct {
	ExprImpl
	Index int
	Ident string
	Step  int
}

func (*IncrExpr) Dump

func (expr *IncrExpr) Dump(prefix string) string

func (*IncrExpr) Init

func (expr *IncrExpr) Init(w *Walker) error

func (*IncrExpr) Run

func (expr *IncrExpr) Run(store *Store) (interface{}, error)

type Init

type Init interface {
	Init(*Walker) error
}

type Lexer

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

func NewLexer

func NewLexer(s *Scanner) *Lexer

func (*Lexer) Error

func (l *Lexer) Error(msg string)

func (*Lexer) Lex

func (l *Lexer) Lex(lval *yySymType) int

type Loader

type Loader func(*Store, string) ([]Stmt, error)

type MethodExpr

type MethodExpr struct {
	ExprImpl
	Expr Expr
	Name string
	Args []Expr
}

func (*MethodExpr) Dump

func (expr *MethodExpr) Dump(prefix string) string

func (*MethodExpr) Init

func (expr *MethodExpr) Init(w *Walker) error

func (*MethodExpr) Run

func (expr *MethodExpr) Run(store *Store) (interface{}, error)

type OutputHandler

type OutputHandler func(*Store, ...interface{})

type Pos

type Pos interface {
	Position() Position
	SetPosition(Position)
}

type PosImpl

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

func (*PosImpl) Position

func (x *PosImpl) Position() Position

func (*PosImpl) SetPosition

func (x *PosImpl) SetPosition(pos Position)

type Position

type Position struct {
	Line   int
	Column int
}

type Range

type Range interface {
	Range() interface{}
}

type ReturnStmt

type ReturnStmt struct {
	StmtImpl
	Expr Expr // nil
}

func (*ReturnStmt) Dump

func (stmt *ReturnStmt) Dump(prefix string) string

func (*ReturnStmt) Init

func (stmt *ReturnStmt) Init(w *Walker) error

func (*ReturnStmt) Run

func (stmt *ReturnStmt) Run(store *Store) (interface{}, error)

type Run

type Run interface {
	Run(*Store) (interface{}, error)
}

type Scanner

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

func NewScanner

func NewScanner(src string) *Scanner

func (*Scanner) Scan

func (s *Scanner) Scan() (tok int, lit string, pos Position, err error)

type Slice

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

func GetSlice

func GetSlice(size int) *Slice

func (*Slice) Get

func (s *Slice) Get() []interface{}

func (*Slice) Put

func (s *Slice) Put()

func (*Slice) Set

func (s *Slice) Set(k int, v interface{})

type Stmt

type Stmt interface {
	Pos
	Run
	Init
	Dump
}

func Parse

func Parse(s *Scanner) ([]Stmt, error)

func ParseSrc

func ParseSrc(src string) ([]Stmt, error)

type StmtImpl

type StmtImpl struct {
	PosImpl
}

type Store

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

func NewStore

func NewStore(funcs map[string]Func, methods map[string]map[string]Func) *Store

func (*Store) Fork

func (store *Store) Fork() *Store

func (*Store) GetArgs

func (store *Store) GetArgs() []interface{}

func (*Store) GetFunc

func (store *Store) GetFunc(name string) Func

func (*Store) GetLocation

func (store *Store) GetLocation() *time.Location

func (*Store) GetMethod

func (store *Store) GetMethod(typ, name string) Func

func (*Store) GetVar

func (store *Store) GetVar(name string, index int) interface{}

func (*Store) GetVar0

func (store *Store) GetVar0() interface{}

func (*Store) Load

func (store *Store) Load(name string) error

func (*Store) Output

func (store *Store) Output(v ...interface{})

func (*Store) Reset

func (store *Store) Reset()

func (*Store) ResetFlags

func (store *Store) ResetFlags()

func (*Store) ResetVars

func (store *Store) ResetVars()

func (*Store) SetForkHandler

func (store *Store) SetForkHandler(forkHandler ForkHandler)

func (*Store) SetLoader

func (store *Store) SetLoader(loader Loader)

func (*Store) SetLocation

func (store *Store) SetLocation(name string) error

func (*Store) SetOutputHandler

func (store *Store) SetOutputHandler(outputHandler OutputHandler)

func (*Store) SetVar

func (store *Store) SetVar(name string, index int, value interface{})

func (*Store) SetVar0

func (store *Store) SetVar0(val interface{})

type StorePool

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

func NewStorePool

func NewStorePool(funcs map[string]Func, methods map[string]map[string]Func) *StorePool

func (*StorePool) Get

func (sp *StorePool) Get() *Store

func (*StorePool) Put

func (sp *StorePool) Put(store *Store)

type Token

type Token struct {
	PosImpl
	Tok int
	Lit string
}

type TypeExpr

type TypeExpr struct {
	ExprImpl
	Kind  int
	Value interface{}
}

func (*TypeExpr) Dump

func (expr *TypeExpr) Dump(prefix string) string

func (*TypeExpr) Init

func (expr *TypeExpr) Init(w *Walker) error

func (*TypeExpr) Run

func (expr *TypeExpr) Run(store *Store) (interface{}, error)

type TypeName

type TypeName interface {
	TypeName() string
}

type Walker

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

func NewWalker

func NewWalker() *Walker

func (*Walker) Fork

func (w *Walker) Fork() *Walker

func (*Walker) GetIndex

func (w *Walker) GetIndex(ident string, check bool) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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