Pegmatite
 All Classes Functions Variables Enumerations
ast.cc
1 /*
2  * Copyright (c) 2014 David Chisnall
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 #include "ast.hh"
22 #include <sstream>
23 
24 
25 namespace AST
26 {
27 void Literal::construct(const pegmatite::InputRange &r,
28  pegmatite::ASTStack &st)
29 {
30  std::stringstream stream;
31  for (char c : r)
32  {
33  stream << c;
34  }
35  stream >> value;
36 }
37 void LocalRegister::construct(const pegmatite::InputRange &r,
38  pegmatite::ASTStack &st)
39 {
40  // Skip the leading l
41  registerNumber = *(++r.begin()) - '0';
42 }
43 void GlobalRegister::construct(const pegmatite::InputRange &r,
44  pegmatite::ASTStack &st)
45 {
46  // Skip the leading g
47  registerNumber = *(++r.begin()) - '0';
48 }
49 void Op::construct(const pegmatite::InputRange &r,
50  pegmatite::ASTStack &st)
51 {
52  // If it's a one-character value:
53  if (++r.begin() == r.end())
54  {
55  switch (*r.begin())
56  {
57  case '=': op = Assign ; break;
58  case '+': op = Add ; break;
59  case '-': op = Sub ; break;
60  case '*': op = Mul ; break;
61  case '/': op = Div ; break;
62  }
63  }
64  else
65  {
66  if (*(++r.begin()) == 'a')
67  {
68  op = Max;
69  }
70  else
71  {
72  op = Min;
73  }
74  }
75 }
76 }
uint16_t value
The value for this literal.
Definition: ast.hh:112
int registerNumber
The number of the global register that this references.
Definition: ast.hh:182
Definition: ast.hh:26
int registerNumber
The number of the local register that this references.
Definition: ast.hh:164