calc

package
v1.0.2201 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ILLEGAL = "ILLEGAL"
	EOF     = "EOF"
	FLOAT   = "FLOAT"

	PLUS     = "+"
	MINUS    = "-"
	ASTERISK = "*"
	SLASH    = "/"

	LPAREN = "("
	RPAREN = ")"
)
View Source
const (
	LOWEST  int
	SUM     // +, -
	PRODUCT // *, /
	PREFIX  // -X
	CALL    // (X)
)

Variables

This section is empty.

Functions

func Calc

func Calc(input string) (float64, error)

四则运算

func CalcAndCompare

func CalcAndCompare(input string) (float64, error)

四则运算和比较运算

func Eval

func Eval(exp Expression) float64

Types

type Expression

type Expression interface {
	String() string
}

type InfixExpression

type InfixExpression struct {
	Token    Token
	Left     Expression
	Operator string
	Right    Expression
}

func (*InfixExpression) String

func (ie *InfixExpression) String() string

type IntegerLiteralExpression

type IntegerLiteralExpression struct {
	Token Token
	Value float64
}

func (*IntegerLiteralExpression) String

func (il *IntegerLiteralExpression) String() string

type Lexer

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

词法分析器

func NewLex

func NewLex(input string) *Lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

词法分析器的核心函数,用于获取下一个词元。针对不同字符返回不同的Token结构值

type Parser

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

func NewParser

func NewParser(l *Lexer) *Parser

func (*Parser) Errors

func (p *Parser) Errors() []string

func (*Parser) ParseExpression

func (p *Parser) ParseExpression(precedence int) Expression

type PrefixExpression

type PrefixExpression struct {
	Token    Token
	Operator string
	Right    Expression
}

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

type Token

type Token struct {
	Type    string //对应我们上面的词元类型
	Literal string // 实际的string字符
}

词元

Jump to

Keyboard shortcuts

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