File: vars.cpp

package info (click to toggle)
qlogo 0.92-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,288 kB
  • sloc: cpp: 16,967; makefile: 6
file content (140 lines) | stat: -rw-r--r-- 3,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

//===-- qlogo/vars.cpp - Vars class implementation -------*- C++ -*-===//
//
// This file is part of QLogo.
//
// QLogo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// QLogo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with QLogo.  If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of the Vars class, which holds the
/// variables in the QLogo language.
///
//===----------------------------------------------------------------------===//

#include "vars.h"
#include "QDebug"
#include <QSet>

const QString tf = "*tf*";

Vars::Vars() { upScope(); }

void Vars::setDatumForName(DatumP &aDatum, const QString &name) {
  for (auto &variables : levels) {
    if (variables.contains(name)) {
      variables[name] = aDatum;
      return;
    }
  }
  levels.last().insert(name, aDatum);
}

DatumP Vars::datumForName(const QString &name) {
  for (auto &variables : levels) {
    auto result = variables.find(name);
    if (result != variables.end()) {
      return *result;
    }
  }
  return nothing;
}

void Vars::setVarAsLocal(const QString &name) {
  levels.first().insert(name, nothing);
}

void Vars::setVarAsGlobal(const QString &name) {
  levels.last().insert(name, nothing);
}

void Vars::upScope() {
  QHash<QString, DatumP> a;
  levels.push_front(a);
}

void Vars::downScope() { levels.pop_front(); }

int Vars::currentScope() { return levels.size(); }

bool Vars::doesExist(const QString &name) {
  for (auto &variables : levels) {
    if (variables.contains(name))
      return true;
  }
  return false;
}

DatumP Vars::allVariables(showContents_t showWhat) {
  List *retval = new List;
  QSet<const QString> seenVars;

  for (auto &levelIter : levels) {
    for (auto &varname : levelIter.keys()) {
      if (!seenVars.contains(varname)) {
        seenVars.insert(varname);

        if (shouldInclude(showWhat, varname))
          retval->append(DatumP(new Word(varname)));
      }
    }
  }

  return DatumP(retval);
}

void Vars::eraseAll() {
  for (auto &levelIter : levels) {
    for (auto &varname : levelIter.keys()) {
      if (!isBuried(varname))
        levelIter.remove(varname);
    }
  }
}

void Vars::eraseVar(const QString &name) {
  for (auto &variables : levels) {
    if (variables.remove(name) > 0)
      return;
  }
}

void Vars::setTest(bool isTrue) {
  DatumP t = new Word(isTrue ? 0 : 1);
  levels.first().insert(tf, t);
}

bool Vars::isTested() { return datumForName(tf).isWord(); }

bool Vars::isTrue() {
  DatumP retval = datumForName(tf);
  if (retval.isWord() && (retval.wordValue()->numberValue() == 0))
    return true;
  return false;
}

bool Vars::isFalse() {
  DatumP retval = datumForName(tf);
  if (retval.isWord() && (retval.wordValue()->numberValue() == 1))
    return true;
  return false;
}

Scope::Scope(Vars *aVars) {
  v = aVars;
  v->upScope();
}

Scope::~Scope() { v->downScope(); }