blob: c4171919b43ef62d092c29d9ae9c25f81e62f0cd [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
swissChili07d325f2021-12-08 20:02:05 -080041 if (from >= list.length())
42 return prime;
43
swissChili3e98c062021-12-04 22:07:38 -080044 // I guess we'll just panic if it's too long
45 // TODO: ERROR HANDLING
swissChilic71acc62021-12-07 08:03:37 -080046 for (int i = 0; i < to - from; i++)
47 {
swissChili3e98c062021-12-04 22:07:38 -080048 prime.append(list[from + i]);
49 }
50
51 return prime;
52}
53
swissChili7babd922021-12-02 22:46:48 -080054MatchResult match(QList<Token> data, QList<Token> pattern, VarContext context);