Commit inicial
This commit is contained in:
commit
e3bd2658cd
5 changed files with 512 additions and 0 deletions
69
input.go
Normal file
69
input.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
index int
|
||||
input []Lexeme
|
||||
}
|
||||
|
||||
func (in Input) Index() int {
|
||||
return in.index
|
||||
}
|
||||
|
||||
func (in Input) String() string {
|
||||
_ = in.input[in.index]
|
||||
return fmt.Sprintf("%v", in.input[in.index:])
|
||||
}
|
||||
|
||||
func (in *Input) Len() int {
|
||||
// _ = in.input[in.index]
|
||||
return len(in.input[in.index:])
|
||||
}
|
||||
|
||||
func (in *Input) Peek(i int) Lexeme {
|
||||
return in.input[in.index+i]
|
||||
}
|
||||
|
||||
func (in *Input) Pop() Lexeme {
|
||||
v := in.input[in.index]
|
||||
in.index++
|
||||
return v
|
||||
}
|
||||
|
||||
func (in *Input) Seek(i int) {
|
||||
_ = in.input[i]
|
||||
in.index = i
|
||||
}
|
||||
|
||||
func (in *Input) Take(amount int) *Input {
|
||||
var out = &Input{
|
||||
index: 0,
|
||||
input: in.input[in.index : in.index+amount],
|
||||
}
|
||||
in.index += amount
|
||||
return out
|
||||
}
|
||||
|
||||
func (in *Input) Find(open, close *Token) (int, bool) {
|
||||
level := 1
|
||||
_ = in.input[in.index]
|
||||
idx := slices.IndexFunc(in.input[in.index:], func(a Lexeme) bool {
|
||||
switch {
|
||||
case a.Equals(open):
|
||||
level++
|
||||
case a.Equals(close):
|
||||
level--
|
||||
return level == 0
|
||||
}
|
||||
return false
|
||||
})
|
||||
if idx > -1 {
|
||||
return idx, true
|
||||
} else {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue