Skip to content

Commit 8b42b70

Browse files
shaerthomasshaerthomas
shaerthomas
authored and
shaerthomas
committed
Created unique stack types for types and values
1 parent 8caae6a commit 8b42b70

File tree

12 files changed

+218
-98
lines changed

12 files changed

+218
-98
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"-g",
1212
"main.cpp",
1313
"-o",
14-
"${fileDirname}/${fileBasenameNoExtension}"
14+
"${fileDirname}/main"
1515
],
1616
"options": {
1717
"cwd": "${fileDirname}"

linkedlist

-37 KB
Binary file not shown.

linkedlist.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Registering new identifiers (and values)
1515

1616

1717

18-
19-
18+
// represents none value
19+
struct NoneValue {};
2020

2121

2222

main

312 Bytes
Binary file not shown.

main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
#include "string"
99

1010

11-
1211
int main(int argc, char* argv[])
1312
{
1413

15-
runStackTests();
14+
runValueStackTests();
1615
runLinkedListTests();
1716
/*
1817
TODO.
19-
Replace LinkedList with something faster
2018
Make it so that you don't have to explicitly pass in a linkedlist item to Execute.
2119
Add scoping.
2220

node

-37 KB
Binary file not shown.

node.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include "linkedlist.h"
2-
#include "stack.h"
1+
#include "symboltable.h"
32

43

54

stack

-37 KB
Binary file not shown.

stack.h

Lines changed: 0 additions & 90 deletions
This file was deleted.

symboltable.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "linkedlist.h"
2+
#include "valuestack.h"
3+
#include "typestack.h"
4+
5+
/*
6+
7+
Symbol table is a stack of linked lists.
8+
The top node of the stack is the current scope.
9+
10+
Enter scope.
11+
When entering a scope, we push new stack node containing a linkedlist onto the stack.
12+
13+
Leave scope.
14+
When leaving a scope, we pop the stack.
15+
16+
17+
Referencing a variable.
18+
Starting from top of stack, we search each linkedlist for the id
19+
20+
Assigning a variable.
21+
Starting from top of stack, we search each linkedlist for a id and update it if we find it.
22+
If we can't find the id, then go back to top of stack and declare variable on that (current scope basically)
23+
24+
*/
25+
26+
27+
void runSymbolTableTests() {
28+
// using storage0 = StackEmptyNode;
29+
//using storage1 = StackNode<LinkedListEmptyNode>;
30+
//using storageN = StackNode<3, StackNode<4, StackNode<9>>>;
31+
32+
}

typestack.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <iostream>
2+
3+
4+
// push
5+
6+
// pop
7+
8+
// walk through
9+
10+
// similar to linked list but without id + pop support
11+
12+
13+
14+
//Linked list empty node type (used for matching end of linkedlist)
15+
struct TypeStackEmptyNode {};
16+
17+
//TypeStack node
18+
template<typename Value, typename T = TypeStackEmptyNode>
19+
struct TypeStackNode {};
20+
21+
22+
23+
/*
24+
Pushing to TypeStack
25+
*/
26+
27+
template<typename T, typename NewValue>
28+
struct TypeStackPushNode {
29+
using newList = T;
30+
};
31+
32+
//edge case for when trying to add to a empty
33+
template<typename NewValue>
34+
struct TypeStackPushNode<TypeStackEmptyNode, NewValue> {
35+
using newList = TypeStackNode<NewValue>;
36+
};
37+
38+
39+
template<template <typename, typename> typename A, typename OldValue, typename T, typename NewValue>
40+
struct TypeStackPushNode<A<OldValue, T>, NewValue> {
41+
using newList = TypeStackNode<NewValue, A<OldValue, T>>;
42+
};
43+
44+
/*
45+
Pop node
46+
*/
47+
48+
template<typename T>
49+
struct TypeStackPopNode {
50+
using newList = T;
51+
};
52+
53+
// //edge case for when trying to add to a empty
54+
template<>
55+
struct TypeStackPopNode<TypeStackEmptyNode> {
56+
using newList = TypeStackEmptyNode;
57+
};
58+
59+
60+
template<template <typename, typename> typename A, typename OldValue, typename T>
61+
struct TypeStackPopNode<A<OldValue, T>> {
62+
using newList = T;
63+
};
64+
65+
66+
void runTypeStackTests() {
67+
using storage0 = TypeStackEmptyNode;
68+
using storage1 = TypeStackNode<int>;
69+
using storageN = TypeStackNode<int, TypeStackNode<float, TypeStackNode<double>>>;
70+
71+
// push tests
72+
73+
//0 cases
74+
static_assert(std::is_same_v<TypeStackNode<int>, TypeStackPushNode<storage0, int>::newList>);
75+
76+
// //1 cases
77+
static_assert(std::is_same_v<TypeStackNode<float, TypeStackNode<int>>, TypeStackPushNode<storage1, float>::newList>);
78+
79+
// //n cases
80+
static_assert(std::is_same_v<TypeStackNode<std::string, TypeStackNode<int, TypeStackNode<float, TypeStackNode<double>>>>, TypeStackPushNode<storageN, std::string>::newList>);
81+
82+
// // pop tests
83+
84+
// //0 cases
85+
static_assert(std::is_same_v<TypeStackEmptyNode, TypeStackPopNode<storage0>::newList>);
86+
87+
// //1 cases
88+
static_assert(std::is_same_v<TypeStackEmptyNode, TypeStackPopNode<storage1>::newList>);
89+
90+
// //n cases
91+
static_assert(std::is_same_v<TypeStackNode<float, TypeStackNode<double>>, TypeStackPopNode<storageN>::newList>);
92+
}

valuestack.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// push
2+
3+
// pop
4+
5+
// walk through
6+
7+
// similar to linked list but without id + pop support
8+
9+
10+
11+
//Linked list empty node type (used for matching end of linkedlist)
12+
struct ValueStackEmptyNode {};
13+
14+
//ValueStack node
15+
template<auto Value, typename T = ValueStackEmptyNode>
16+
struct ValueStackNode {};
17+
18+
19+
20+
/*
21+
Pushing to ValueStack
22+
*/
23+
24+
template<typename T, auto NewValue>
25+
struct ValueStackPushNode {
26+
using newList = T;
27+
};
28+
29+
//edge case for when trying to add to a empty
30+
template<auto NewValue>
31+
struct ValueStackPushNode<ValueStackEmptyNode, NewValue> {
32+
using newList = ValueStackNode<NewValue>;
33+
};
34+
35+
36+
template<template <auto, typename> typename A, auto OldValue, typename T, auto NewValue>
37+
struct ValueStackPushNode<A<OldValue, T>, NewValue> {
38+
using newList = ValueStackNode<NewValue, A<OldValue, T>>;
39+
};
40+
41+
/*
42+
Pop node
43+
*/
44+
45+
template<typename T>
46+
struct ValueStackPopNode {
47+
using newList = T;
48+
};
49+
50+
//edge case for when trying to add to a empty
51+
template<>
52+
struct ValueStackPopNode<ValueStackEmptyNode> {
53+
using newList = ValueStackEmptyNode;
54+
};
55+
56+
57+
template<template <auto, typename> typename A, auto OldValue, typename T>
58+
struct ValueStackPopNode<A<OldValue, T>> {
59+
using newList = T;
60+
};
61+
62+
63+
void runValueStackTests() {
64+
using storage0 = ValueStackEmptyNode;
65+
using storage1 = ValueStackNode<3>;
66+
using storageN = ValueStackNode<3, ValueStackNode<4, ValueStackNode<9>>>;
67+
68+
// push tests
69+
70+
//0 cases
71+
static_assert(std::is_same_v<ValueStackNode<40>, ValueStackPushNode<storage0, 40>::newList>);
72+
73+
// //1 cases
74+
static_assert(std::is_same_v<ValueStackNode<4, ValueStackNode<3>>, ValueStackPushNode<storage1, 4>::newList>);
75+
76+
// //n cases
77+
static_assert(std::is_same_v<ValueStackNode<12, ValueStackNode<3, ValueStackNode<4, ValueStackNode<9>>>>, ValueStackPushNode<storageN, 12>::newList>);
78+
79+
// // pop tests
80+
81+
// //0 cases
82+
static_assert(std::is_same_v<ValueStackEmptyNode, ValueStackPopNode<storage0>::newList>);
83+
84+
// //1 cases
85+
static_assert(std::is_same_v<ValueStackEmptyNode, ValueStackPopNode<storage1>::newList>);
86+
87+
// //n cases
88+
static_assert(std::is_same_v<ValueStackNode<4, ValueStackNode<9>>, ValueStackPopNode<storageN>::newList>);
89+
}

0 commit comments

Comments
 (0)