parser

package
v0.0.0-...-47ae25c Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

tokenize includes functions to help create a list of tokens from a given string

Index

Constants

View Source
const (
	ADD = iota
	MINUS
	MULTIPLY
	DIVIDE
	INT
	FLOAT
	LIST
	BOOLEAN
	UNKNOWN
	ERROR
)

The expression might be several types, which are numericals, lists or operations.

View Source
const (
	BEGIN_TOKEN = '('
	END_TOKEN   = ')'
	ADD_TOKEN   = '+'
	MINUS_TOKEN = '-'
	MULTI_TOKEN = '*'
	DIV_TOKEN   = '/'
	BEGIN_EXPR  = "("
	END_EXPR    = ")"
	ADD_EXPR    = "+"
	MINUS_EXPR  = "-"
	MULTI_EXPR  = "*"
	DIV_EXPR    = "/"
)

Variables

This section is empty.

Functions

func EVAL

func EVAL(e *EXPR) (TYPE int, VALUE interface{})

Eval function provides evaluation function for lisp expressions, currently the defini- tion is like this:

If the EXPR is numerical operation, the num- ber of operands must be >= 2, otherwise the interpreter stop evaluating and throw an er- ror

If the EXPR is numerical, the value and its type is returned.

If the EXPR is list, the string of the oper- ands are returned

func EVAL_ADD

func EVAL_ADD(e *EXPR) (TYPE int, VALUE interface{})

ADD function deals with add expressions. We first check if there are any floats in the operands, and if there is, we convert every- thing to float and return float result ***Allows single operands***

func EVAL_DIVIDE

func EVAL_DIVIDE(e *EXPR) (TYPE int, VALUE interface{})

ADD function deals with add expressions. We first check if there are any floats in the operands, and if there is, we convert every- thing to float and return float result ***Allow Single Operands***

func EVAL_MINUS

func EVAL_MINUS(e *EXPR) (TYPE int, VALUE interface{})

MINUS function deals with minus expressions. We first check if there are any floats in the operands, and if there is, we convert every- thing to float and return float result ***Allows single operands***

func EVAL_MULTIPLY

func EVAL_MULTIPLY(e *EXPR) (TYPE int, VALUE interface{})

ADD function deals with add expressions. We first check if there are any floats in the operands, and if there is, we convert every- thing to float and return float result ***Allow Single Operands***

func IS_VALID_NUMBER

func IS_VALID_NUMBER(s string) bool

IS_VALID_NUMBER takes in a string and ch- eck if it is a valid number, i.e. there are no non-numerical letters with in and '.' only appear once

func Tokenize

func Tokenize(s string) []string

Tokenize is a function used to create the list of tokens to work with

Types

type ASTQUEUE

type ASTQUEUE struct {
	ASTELEM []string
}

func ASTQUEUEConstructer

func ASTQUEUEConstructer(tokens []string) *ASTQUEUE

ASTQUEUE constructor using an existing token list, returns the pointer to the constructed astqueue

func (*ASTQUEUE) Pop

func (A *ASTQUEUE) Pop() (string, error)

Returns the first element in the queue and delete it afterwards. If pop from an empty queue, an empty string is re- turned with an error.

func (*ASTQUEUE) ReachedEnd

func (A *ASTQUEUE) ReachedEnd() bool

return the result if there is no more elements in the queue

func (*ASTQUEUE) Top

func (A *ASTQUEUE) Top() (string, error)

Returns the first element currently in the queue, but do not eliminate it. If there is no such element, an empty string and error is returned.

type EXPR

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

func GEN_AST

func GEN_AST(tokens []string) (*EXPR, error)

GEN_AST parses the tokens into an abstr- act syntax tree and if parsed correctly, the root expression is returned. If not, nil is returned with an error.

func PAR_AST

func PAR_AST(q *ASTQUEUE) (*EXPR, error)

PAR_AST receives an ASTQUEUE and deals with the actual parsing job. All exprs and the operands are checked validity and if there are semantic errors, the parsing job stops and return an error. The errors are returned by concatnation

Jump to

Keyboard shortcuts

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