Drafting ProductStatement

This commit is contained in:
斟酌 鵬兄 2022-10-22 00:35:24 +08:00
parent bbde3e80f7
commit 36faffe882
6 changed files with 122 additions and 4 deletions

View File

@ -135,6 +135,54 @@ func ( this *Lexer ) ReadDecimal() ( f float64, err error ) {
return
}
func ( this *Lexer ) ReadKeyword( Keyword string ) ( err error ) {
reader := this.reader
kReader := strings.NewReader( Keyword )
var b strings.Builder
for {
r, _, _err := reader.ReadRune()
err = _err
if err != nil {
if err == io.EOF {
if 0 < b.Len() {
err = nil
} else {
err = fmt.Errorf( "Nothing left to read" )
return
}
}
break
}
if strings.ContainsRune( K_SPACES, r ) && b.Len() == 0 {
continue
}
k, _, _err := kReader.ReadRune()
if _err != nil {
if _err != io.EOF {
err = _err
} else {
err = nil
}
return
}
if k != r {
err = fmt.Errorf(
"Expected \"%s\", got \"%s\"",
Keyword,
this.ReadUntilNot( K_STATEMENT_CHARS, true, r ),
)
return
}
b.WriteRune( r )
}
return
}
func ( this *Lexer ) ReadAlpha() ( s string, err error ) {
reader := this.reader

View File

@ -116,4 +116,15 @@ func TestAlpha( t *testing.T ) {
t.Errorf( "Expected error for %s, got %s", v, f )
}
}
lexer.SetReader( strings.NewReader( "abcd efgh" ) )
f, err := lexer.ReadAlpha()
f, err = lexer.ReadAlpha()
if err != nil {
t.Errorf( "Expected string for \"efgh\", got %s", err )
}
if f != "efgh" {
t.Errorf( "Expected \"efgh\", got %s", f )
}
}

View File

@ -18,10 +18,11 @@ THEN OPT EXPECT: FOR EVERY
THEN EXPECT: [SOLD|BOUGHT] FROM [ExchangeType]
*/
func BuyStatement( lexer *engine.Lexer ) ( stmtd.IStatement, error ) {
func BuyStatement( lexer *engine.Lexer ) ( istmt stmtd.IStatement, err error ) {
orderStmt := stmtd.OrderStatement{}
orderStmt.Action = "BUY"
// AmountStatement
readAmount := engine.LexerExpect{
Statements: true,
Keywords: false,
@ -32,10 +33,30 @@ func BuyStatement( lexer *engine.Lexer ) ( stmtd.IStatement, error ) {
amountStmt, err := lexer.ReadStatement( &readAmount )
if err != nil {
return nil, err
return
}
orderStmt.For( amountStmt )
var istmt stmtd.IStatement = orderStmt
return istmt, err
// OF
err = lexer.ReadKeyword( "OF" )
if err != nil {
return
}
// ProductStatement
readProduct := engine.LexerExpect{
Statements: true,
Keywords: false,
Brackets: false,
Quotes: false,
Key: "PRODUCT",
}
productStmt, err := lexer.ReadStatement( &readProduct )
if err != nil {
return
}
orderStmt.Product( productStmt )
istmt = orderStmt
return
}

View File

@ -12,6 +12,7 @@ func TestBuyStatement( t *testing.T ) {
supportedStmts := map[string] func( *engine.Lexer ) ( stmtd.IStatement, error ) {
"BUY": BuyStatement,
"AMOUNT": AmountStatement,
"PRODUCT": ProductStatement,
}
lexer.SupportedStmts = &supportedStmts

View File

@ -0,0 +1,26 @@
package actions
import (
// "fmt"
engine "github.com/tgckpg/mmqlengine/mmql/engine"
stmtd "github.com/tgckpg/mmqlengine/mmql/statements"
)
/*
EXPECT: [NAME]
OR
EXPECT: [FUNCTION]( ...params )
*/
func ProductStatement( lexer *engine.Lexer ) ( istmt stmtd.IStatement, err error ) {
stmt := stmtd.ProductStatement{}
name, err := lexer.ReadAlpha()
if err != nil {
return
}
stmt.Name = name
istmt = stmt
return
}

View File

@ -25,8 +25,16 @@ type AmountStatement struct {
Unit string
}
type ProductStatement struct {
IStatement
Name string
}
type IActionStatement interface { }
type OrderStatement struct {
IStatement
IActionStatement
@ -35,3 +43,6 @@ type OrderStatement struct {
func ( this *OrderStatement ) For( stmt IStatement ) {
}
func ( this *OrderStatement ) Product( stmt IStatement ) {
}