MMQLEngine/mmql/engine/parsers/buy.go

97 lines
2.3 KiB
Go
Raw Normal View History

2022-10-21 21:58:19 +00:00
package parsers
import (
engine "github.com/tgckpg/mmqlengine/mmql/engine"
stmtd "github.com/tgckpg/mmqlengine/mmql/statements"
)
/*
EXPECT: BUY
THEN EXPECT: [AmountStatement] OF [FinancialInstrumentStatement]
THEN EXPECT: FOR [AmountStatement]
THEN EXPECT: FROM [ExchangeType]
THEN OPT EXPECT: [ExchangeType]
THEN OPT EXPECT: [AND|OR]
THEN EXPECT: [ActionExpression]
THEN OPT EXPECT: FOR EVERY
THEN EXPECT: [AmountType] OF [FinancialInstrumentType]
THEN EXPECT: [SOLD|BOUGHT] FROM [ExchangeType]
*/
func BuyStatement( lexer *engine.Lexer ) ( istmt stmtd.IStatement, err error ) {
orderStmt := stmtd.OrderStatement{}
orderStmt.Action = "BUY"
// AmountStatement
readAmount := engine.LexerExpect{
Statements: true,
Keywords: false,
Brackets: false,
Quotes: false,
Key: "AMOUNT",
}
_amountStmt, err := lexer.ReadStatement( &readAmount )
if err != nil {
return
}
amountStmt := any( _amountStmt ).( stmtd.AmountStatement )
orderStmt.SetAmount( &amountStmt )
var fiStmt stmtd.FinancialInstrumentStatement
// OF
err = lexer.ReadKeyword( "OF" )
if err != nil {
// try read FOR
err = lexer.ReadKeyword( "FOR" )
if err != nil {
return
}
// BUY 1 QQQ
fiStmt := stmtd.FinancialInstrumentStatement{}
fiStmt.Name = amountStmt.Unit()
// Equivalent to BUY 1 UNIT OF QQQ
amountStmt.SetUnit( "UNITS" )
} else {
// BUY 1 UNIT OF QQQ
readFI := engine.LexerExpect{
Statements: true,
Keywords: false,
Brackets: false,
Quotes: false,
Key: "FINANCIAL_INSTRUMENT",
}
_fiStmt, _err := lexer.ReadStatement( &readFI )
if _err != nil {
err = _err
return
}
fiStmt = any( _fiStmt ).( stmtd.FinancialInstrumentStatement )
// FOR
err = lexer.ReadKeyword( "FOR" )
if err != nil {
return
}
}
orderStmt.SetFinancialInstrument( &fiStmt )
// FOR [AmountStatement]
_forStmt, err := lexer.ReadStatement( &readAmount )
if err != nil {
return
}
forStmt := any( _forStmt ).( stmtd.AmountStatement )
orderStmt.SetFor( &forStmt )
istmt = orderStmt
return
}