adventlang

package
v0.0.0-...-a894a00 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const VERSION = 0.1

Variables

This section is empty.

Functions

func GetGrammer

func GetGrammer() string

func InjectRuntime

func InjectRuntime(context *Context)

Given a root content, add runtime functions to the module's scope

func ReadProgram

func ReadProgram(filename string) string

Types

type Addition

type Addition struct {
	Pos lexer.Position

	Multiplication *Multiplication `@@`
	Op             *string         `[ @( "-" | "+" )`
	Next           *Addition       `  @@ ]`
}

func (Addition) Equals

func (addition Addition) Equals(other Value) (bool, error)

func (Addition) Eval

func (addition Addition) Eval(frame *StackFrame) (Value, error)

func (Addition) String

func (addition Addition) String() string

type Assignment

type Assignment struct {
	Pos lexer.Position

	Let     *string     `@"let"?`
	LogicOr *LogicOr    `@@`
	Op      *string     `( @"="`
	Next    *Assignment `  @@ )?`
}

func (Assignment) Equals

func (assignment Assignment) Equals(other Value) (bool, error)

func (Assignment) Eval

func (assignment Assignment) Eval(frame *StackFrame) (Value, error)

func (Assignment) String

func (assignment Assignment) String() string

type BoolValue

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

func (BoolValue) Equals

func (boolValue BoolValue) Equals(other Value) (bool, error)

func (BoolValue) String

func (boolValue BoolValue) String() string

type BreakError

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

func (BreakError) Error

func (b BreakError) Error() string

type Call

type Call struct {
	Pos lexer.Position

	Ident     *string    `@Ident`
	CallChain *CallChain `@@`
}

func (Call) Equals

func (call Call) Equals(other Value) (bool, error)

func (Call) Eval

func (call Call) Eval(frame *StackFrame) (Value, error)

func (Call) String

func (call Call) String() string

type CallArgs

type CallArgs struct {
	Exprs []*Expr `"(" (@@ ("," @@)*)? ")"`
}

type CallChain

type CallChain struct {
	Pos lexer.Position

	Args     *CallArgs     `( @@`
	Index    *CallIndex    ` | @@`
	Property *CallProperty ` | @@ )`
	Next     *CallChain    `@@?`
}

type CallIndex

type CallIndex struct {
	Expr *Expr `"[" @@ "]"`
}

type CallProperty

type CallProperty struct {
	Ident *string `"." @Ident`
}

type Comparison

type Comparison struct {
	Pos lexer.Position

	Addition *Addition   `@@`
	Op       *string     `[ @( ">" "=" | ">" | "<" "=" | "<" )`
	Next     *Comparison `  @@ ]`
}

func (Comparison) Equals

func (comparison Comparison) Equals(other Value) (bool, error)

func (Comparison) Eval

func (comparison Comparison) Eval(frame *StackFrame) (Value, error)

func (Comparison) String

func (comparison Comparison) String() string

type Context

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

func RunProgram

func RunProgram(filename string, source string) (string, *Context, error)

func (*Context) Init

func (context *Context) Init(filename string)

type ContinueError

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

func (ContinueError) Error

func (c ContinueError) Error() string

type DictKV

type DictKV struct {
	Pos lexer.Position

	KeyExpr   *Expr   `( @@ |`
	KeyStr    *string `"'" @Ident "'")`
	ValueExpr *Expr   `":" @@`
}

type DictLiteral

type DictLiteral struct {
	Pos lexer.Position

	Items []*DictKV `"{" ( @@ ( "," @@ )* )? "}"`
}

func (DictLiteral) Equals

func (dictLiteral DictLiteral) Equals(other Value) (bool, error)

func (DictLiteral) Eval

func (dictLiteral DictLiteral) Eval(frame *StackFrame) (Value, error)

func (DictLiteral) String

func (dictLiteral DictLiteral) String() string

type DictValue

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

func (*DictValue) Delete

func (dictValue *DictValue) Delete(key string)

func (DictValue) Equals

func (dictValue DictValue) Equals(other Value) (bool, error)

func (*DictValue) Get

func (dictValue *DictValue) Get(key string) (*Value, error)

func (*DictValue) Set

func (dictValue *DictValue) Set(key string, value Value) *Value

func (DictValue) String

func (dictValue DictValue) String() string

type Equality

type Equality struct {
	Pos lexer.Position

	Comparison *Comparison `@@`
	Op         *string     `[ @( "!" "=" | "=" "=" )`
	Next       *Equality   `  @@ ]`
}

func (Equality) Equals

func (equality Equality) Equals(other Value) (bool, error)

func (Equality) Eval

func (equality Equality) Eval(frame *StackFrame) (Value, error)

func (Equality) String

func (equality Equality) String() string

type Expr

type Expr struct {
	Pos lexer.Position

	Assignment *Assignment `@@`
}

func (Expr) Equals

func (expr Expr) Equals(other Value) (bool, error)

func (Expr) Eval

func (expr Expr) Eval(frame *StackFrame) (Value, error)

func (Expr) String

func (expr Expr) String() string

type ForStatement

type ForStatement struct {
	Pos lexer.Position

	Init      *Expr        `"for" "(" @@? ";"`
	Condition *Expr        `@@? ";"`
	Post      *Expr        `@@? ")"`
	Block     []*Statement `"{" @@* "}"`
}

func (ForStatement) Equals

func (forStatement ForStatement) Equals(other Value) (bool, error)

func (ForStatement) Eval

func (forStatement ForStatement) Eval(frame *StackFrame) (Value, error)

func (ForStatement) String

func (forStatement ForStatement) String() string

type FuncLiteral

type FuncLiteral struct {
	Pos lexer.Position

	Params []string     `"func" "(" ( @Ident ( "," @Ident )* )? ")"`
	Block  []*Statement `"{" @@* "}"`
}

func (FuncLiteral) Equals

func (functionLiteral FuncLiteral) Equals(other Value) (bool, error)

func (FuncLiteral) Eval

func (functionLiteral FuncLiteral) Eval(frame *StackFrame) (Value, error)

func (FuncLiteral) String

func (functionLiteral FuncLiteral) String() string

type FunctionValue

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

func (FunctionValue) Equals

func (functionValue FunctionValue) Equals(other Value) (bool, error)

func (FunctionValue) Exec

func (functionValue FunctionValue) Exec(position string, args []Value) (Value, error)

func (FunctionValue) String

func (functionValue FunctionValue) String() string

type IdentifierValue

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

func (IdentifierValue) Equals

func (identifierValue IdentifierValue) Equals(other Value) (bool, error)

func (IdentifierValue) String

func (identifierValue IdentifierValue) String() string

type IfStatement

type IfStatement struct {
	Pos lexer.Position

	Condition *Expr        `"if" "(" @@ ")"`
	If        []*Statement `"{" @@* "}"`
	Else      []*Statement `("else" "{" @@* "}")?`
}

func (IfStatement) Equals

func (ifStatement IfStatement) Equals(other Value) (bool, error)

func (IfStatement) Eval

func (ifStatement IfStatement) Eval(frame *StackFrame) (Value, error)

func (IfStatement) String

func (ifStatement IfStatement) String() string

type ListLiteral

type ListLiteral struct {
	Pos lexer.Position

	Items []*Expr `"[" ( @@ ( "," @@ )* )? "]"`
}

func (ListLiteral) Equals

func (listLiteral ListLiteral) Equals(other Value) (bool, error)

func (ListLiteral) Eval

func (listLiteral ListLiteral) Eval(frame *StackFrame) (Value, error)

func (ListLiteral) String

func (listLiteral ListLiteral) String() string

type ListValue

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

func (ListValue) Append

func (listValue ListValue) Append(other Value)

func (ListValue) Equals

func (listValue ListValue) Equals(other Value) (bool, error)

func (*ListValue) Get

func (listValue *ListValue) Get(index int) (Value, error)

func (ListValue) Popat

func (listValue ListValue) Popat(index int) (Value, error)

func (ListValue) Prepend

func (listValue ListValue) Prepend(other Value)

func (ListValue) String

func (listValue ListValue) String() string

type LogicAnd

type LogicAnd struct {
	Pos lexer.Position

	Equality *Equality `@@`
	Op       *string   `( @"and"`
	Next     *LogicAnd `  @@ )?`
}

func (LogicAnd) Equals

func (logicAnd LogicAnd) Equals(other Value) (bool, error)

func (LogicAnd) Eval

func (logicAnd LogicAnd) Eval(frame *StackFrame) (Value, error)

func (LogicAnd) String

func (logicAnd LogicAnd) String() string

type LogicOr

type LogicOr struct {
	Pos lexer.Position

	LogicAnd *LogicAnd `@@`
	Op       *string   `( @"or"`
	Next     *LogicOr  `  @@ )?`
}

func (LogicOr) Equals

func (logicOr LogicOr) Equals(other Value) (bool, error)

func (LogicOr) Eval

func (logicOr LogicOr) Eval(frame *StackFrame) (Value, error)

func (LogicOr) String

func (logicOr LogicOr) String() string

type Multiplication

type Multiplication struct {
	Pos lexer.Position

	Unary *Unary          `@@`
	Op    *string         `[ @( "/" | "*" | "%" )`
	Next  *Multiplication `  @@ ]`
}

func (Multiplication) Equals

func (multiplication Multiplication) Equals(other Value) (bool, error)

func (Multiplication) Eval

func (multiplication Multiplication) Eval(frame *StackFrame) (Value, error)

func (Multiplication) String

func (multiplication Multiplication) String() string

type NativeFunctionValue

type NativeFunctionValue struct {
	Exec func(*StackFrame, string, []Value) (Value, error)
	// contains filtered or unexported fields
}

func (NativeFunctionValue) Equals

func (nativeFunctionValue NativeFunctionValue) Equals(other Value) (bool, error)

func (NativeFunctionValue) String

func (nativeFunctionValue NativeFunctionValue) String() string

type NumberValue

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

func (NumberValue) Equals

func (numberValue NumberValue) Equals(other Value) (bool, error)

func (NumberValue) String

func (numberValue NumberValue) String() string

type Primary

type Primary struct {
	Pos lexer.Position

	FuncLiteral   *FuncLiteral   `@@`
	ListLiteral   *ListLiteral   `| @@`
	DictLiteral   *DictLiteral   `| @@`
	Call          *Call          `| @@`
	SubExpression *SubExpression `| @@`
	Number        *float64       `| ( @Float | @Int )`
	Str           *string        `| @String`
	True          *bool          `| @"true"`
	False         *bool          `| @"false"`
	Undefined     *string        `| @"undefined"`
	Ident         *string        `| @Ident`
}

func (Primary) Eval

func (primary Primary) Eval(frame *StackFrame) (Value, error)

func (Primary) String

func (primary Primary) String() string

type Program

type Program struct {
	Pos lexer.Position

	Statements []*Statement `@@*`
}

func GenerateAST

func GenerateAST(source string) (*Program, error)

func (Program) Equals

func (program Program) Equals(other Value) (bool, error)

func (Program) Eval

func (program Program) Eval(frame *StackFrame) (Value, error)

func (Program) String

func (program Program) String() string

type ReferenceValue

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

Sometimes we want to bubble up a reference to a list or dict item so that it can be reassigned. Use `unref` to turn into a plain value

func (ReferenceValue) Equals

func (referenceValue ReferenceValue) Equals(other Value) (bool, error)

func (ReferenceValue) String

func (referenceValue ReferenceValue) String() string

type ReturnError

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

func (ReturnError) Error

func (r ReturnError) Error() string

type ReturnStatement

type ReturnStatement struct {
	Pos lexer.Position

	Expr *Expr `"return" @@?`
}

type StackFrame

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

func (*StackFrame) Get

func (frame *StackFrame) Get(key string) (Value, error)

Get a variable's value by looking through every scope (bottom to top)

func (*StackFrame) GetChild

func (frame *StackFrame) GetChild(trace string) *StackFrame

func (*StackFrame) Set

func (frame *StackFrame) Set(key string, value Value)

Set a variable by looking through every scope (bottom to top) until an existing variable is found. If there is no matching variable, declare a new variable in the current scope

func (*StackFrame) String

func (frame *StackFrame) String() string

type Statement

type Statement struct {
	Pos lexer.Position

	If    *IfStatement    `@@`
	For   *ForStatement   `| @@`
	While *WhileStatement `| @@`
	// These optional semi-colons could cause problems
	Return   *ReturnStatement `| @@ ";"?`
	Break    *string          `| @"break" ";"?`
	Continue *string          `| @"continue" ";"?`
	// --
	Expr *Expr `| @@ ";"`
}

func (Statement) Equals

func (statement Statement) Equals(other Value) (bool, error)

func (Statement) Eval

func (statement Statement) Eval(frame *StackFrame) (Value, error)

func (Statement) String

func (statement Statement) String() string

type StringValue

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

func (StringValue) Equals

func (stringValue StringValue) Equals(other Value) (bool, error)

func (StringValue) Get

func (strValue StringValue) Get(index int) (Value, error)

func (StringValue) String

func (stringValue StringValue) String() string

type SubExpression

type SubExpression struct {
	Pos lexer.Position

	Expr      *Expr      `"(" @@ ")" `
	CallChain *CallChain `@@?`
}

func (SubExpression) Equals

func (subExpression SubExpression) Equals(other Value) (bool, error)

func (SubExpression) Eval

func (subExpression SubExpression) Eval(frame *StackFrame) (Value, error)

func (SubExpression) String

func (subExpression SubExpression) String() string

type Unary

type Unary struct {
	Pos lexer.Position

	Op      *string  `( @( "!" | "-" )`
	Unary   *Unary   `  @@ )`
	Primary *Primary `| @@`
}

func (Unary) Eval

func (unary Unary) Eval(frame *StackFrame) (Value, error)

type UndefinedValue

type UndefinedValue struct{}

func (UndefinedValue) Equals

func (undefinedValue UndefinedValue) Equals(other Value) (bool, error)

func (UndefinedValue) String

func (undefinedValue UndefinedValue) String() string

type Value

type Value interface {
	String() string
	Equals(Value) (bool, error)
}

Language value ranging from strings, numbers, to functions, lists, and dicts

type WhileStatement

type WhileStatement struct {
	Pos lexer.Position

	Condition *Expr        `"while" "(" @@? ")"`
	Block     []*Statement `"{" @@* "}"`
}

func (WhileStatement) Equals

func (whileStatement WhileStatement) Equals(other Value) (bool, error)

func (WhileStatement) Eval

func (whileStatement WhileStatement) Eval(frame *StackFrame) (Value, error)

func (WhileStatement) String

func (whileStatement WhileStatement) String() string

Jump to

Keyboard shortcuts

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