sammine-lang
Loading...
Searching...
No Matches
LexicalContext.h
Go to the documentation of this file.
1#pragma once
2#include "util/Utilities.h"
3#include <functional>
4#include <memory>
5#include <ostream>
6#include <set>
7
11
14 nameFound,
15 nameNotFound,
16};
17
19template <class T, class Scope> class LexicalContext {
20 std::set<std::string> symbols;
21 std::unordered_map<std::string, T> symbols_to_t;
22
23public:
24 LexicalContext *parent_scope;
25 std::optional<Scope> s;
26
27 explicit LexicalContext() : symbols(), symbols_to_t(), parent_scope() {}
28 LexicalContext(LexicalContext *parent_scope)
29 : symbols(), symbols_to_t(), parent_scope(parent_scope) {}
30
31 void setScope(Scope s) { this->s = s; }
32
33 void registerNameT(const std::string &name, T l) {
34 symbols.insert(name);
35 symbols_to_t[name] = l;
36 }
37 NameQueryResult queryName(const std::string &name) const {
38 return symbols.contains(name) ? nameFound : nameNotFound;
39 }
40 T get_from_name(const std::string &name) const {
41 return symbols_to_t.at(name);
42 }
43
44 T recursive_get_from_name(const std::string &name) const {
45 if (symbols.find(name) != symbols.end())
46 return symbols_to_t.at(name);
47 else if (parent_scope == nullptr) {
48 sammine_util::abort(fmt::format("name {} not found", name));
49 } else
50 return parent_scope->recursive_get_from_name(name);
51 }
52
53 NameQueryResult recursiveQueryName(const std::string &name) const {
54 if (symbols.find(name) != symbols.end())
55 return nameFound;
56 else if (parent_scope == nullptr) {
57 return nameNotFound;
58 } else
59 return parent_scope->recursiveQueryName(name);
60 }
61}; // namespace sammine_lang::AST
NameQueryResult
NameQueryResult enum.
Definition LexicalContext.h:13
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...