blob: 24fc795174576f92be97250d3de118105593aa4f [file] [log] [blame]
swissChili7babd922021-12-02 22:46:48 -08001#pragma once
2
3#include "Token.h"
4#include "VarContext.h"
5
swissChilic71acc62021-12-07 08:03:37 -08006struct MatchResult
7{
swissChili7babd922021-12-02 22:46:48 -08008 bool success;
9 VarContext context;
10};
11
swissChili3e98c062021-12-04 22:07:38 -080012template <typename T>
swissChilic71acc62021-12-07 08:03:37 -080013bool listStartsWith(const QList<T> &haystack, const QList<T> &needle)
14{
swissChili3e98c062021-12-04 22:07:38 -080015 if (needle.length() > haystack.length())
16 return false;
17
swissChilic71acc62021-12-07 08:03:37 -080018 for (int i = 0; i < needle.length(); i++)
19 {
swissChili3e98c062021-12-04 22:07:38 -080020 if (haystack[i] != needle[i])
21 return false;
22 }
23
24 return true;
25}
26
27template <typename T>
swissChilic71acc62021-12-07 08:03:37 -080028void listDrop(QList<T> &list, int n)
29{
30 for (; n; n--)
31 {
swissChili3e98c062021-12-04 22:07:38 -080032 list.removeFirst();
33 }
34}
35
36template <typename T>
swissChilic71acc62021-12-07 08:03:37 -080037QList<T> listSlice(QList<T> &list, int from, int to)
38{
swissChili3e98c062021-12-04 22:07:38 -080039 QList<T> prime;
40
41 // I guess we'll just panic if it's too long
42 // TODO: ERROR HANDLING
swissChilic71acc62021-12-07 08:03:37 -080043 for (int i = 0; i < to - from; i++)
44 {
swissChili3e98c062021-12-04 22:07:38 -080045 prime.append(list[from + i]);
46 }
47
48 return prime;
49}
50
swissChili7babd922021-12-02 22:46:48 -080051MatchResult match(QList<Token> data, QList<Token> pattern, VarContext context);