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 LexicalContext {
20 std::set<std::string> symbols;
21 std::unordered_map<std::string, T> symbols_to_t;
22
23public:
24 LexicalContext *parent_scope;
25
26 explicit LexicalContext() : symbols(), symbols_to_t(), parent_scope() {}
27 LexicalContext(LexicalContext *parent_scope)
28 : symbols(), symbols_to_t(), parent_scope(parent_scope) {}
29
30 void registerNameT(const std::string &name, T l) {
31 symbols.insert(name);
32 symbols_to_t[name] = l;
33 }
34 NameQueryResult queryName(const std::string &name) const {
35 return symbols.contains(name) ? nameFound : nameNotFound;
36 }
37 T get_from_name(const std::string &name) { return symbols_to_t.at(name); }
38
39 T recursive_get_from_name(const std::string &name) {
40 if (symbols.find(name) != symbols.end())
41 return symbols_to_t[name];
42 else if (parent_scope == nullptr) {
43 sammine_util::abort(fmt::format("name {} not found", name));
44 } else
45 return parent_scope->recursive_get_from_name(name);
46 }
47
48 NameQueryResult recursiveQueryName(const std::string &name) const {
49 if (symbols.find(name) != symbols.end())
50 return nameFound;
51 else if (parent_scope == nullptr) {
52 return nameNotFound;
53 } else
54 return parent_scope->recursiveQueryName(name);
55 }
56}; // namespace sammine_lang::AST
NameQueryResult
NameQueryResult enum.
Definition LexicalContext.h:13
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...