-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulas.ts
More file actions
155 lines (142 loc) · 4 KB
/
Formulas.ts
File metadata and controls
155 lines (142 loc) · 4 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Formulas
Copyright (C) 2025 Andreas Kovalski
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* An abstract class requiring every subclass to have a function to accept a {@link FormulaVisitor}
*/
abstract class Formula{
abstract acceptVisitor<T>(visitor : FormulaVisitor<T>): T
}
class Implication extends Formula{
constructor(readonly left : Formula, readonly right : Formula){
super()
}
/**
* @override
* @param visitor
*/
acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitImplication(this)
}
}
class Conjunction extends Formula{
constructor(readonly left : Formula, readonly right : Formula){
super()
}
/**
* @override
* @param visitor
*/
acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitConjunction(this)
}
}
class Disjunction extends Formula{
constructor(readonly left : Formula, readonly right : Formula){
super()
}
/**
* @override
* @param visitor
*/
acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitDisjunction(this)
}
}
class Negation extends Formula{
constructor(readonly formula : Formula){
super()
}
/**
* @override
* @param visitor
*/
acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitNegation(this)
}
}
/**
* Used when parsing user input
*/
type VariableText = string
function isVariableText(thing : any) : thing is VariableText{
function isLetter(char : string){
const charCode = char.charCodeAt(0)
return (65 <= charCode && charCode <= 90 ) || //upper case
(97 <= charCode && charCode <= 122) //lower case
}
function isNumber(char : string){
const charCode = char.charCodeAt(0)
return 48 <= charCode && charCode <= 57
}
function isAlphanumeric(char : string){
return isLetter(char) || isNumber(char)
}
if(typeof thing !== "string") return false
for(let i = 0; i < thing.length; i++)
if(!isAlphanumeric(thing.charAt(i)))return false
return true
}
const alreadyUsedVariables : Map<VariableText,Variable> = new Map()
/**
* @todo right now, if variables use the same name, they are also the same object.
* Maybe think about just using an equals() function instead, to remove {@link alreadyUsedVariables}
* and saving some memory
*/
class Variable extends Formula{
private constructor(readonly name : VariableText){
super()
}
static create(name : VariableText) : Variable{
if(alreadyUsedVariables.has(name)) return alreadyUsedVariables.get(name)!
const newVariable = new Variable(name)
alreadyUsedVariables.set(name, newVariable)
return newVariable
}
/**
* @override
* @param visitor
*/
acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitVariable(this)
}
}
class Truth extends Formula{
private static instance? : Falsity
private constructor(){
super()
}
static create() : Truth{
if(Truth.instance === undefined)
return Truth.instance = new Truth()
return Truth.instance
}
override acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitTruth(this)
}
}
class Falsity extends Formula{
private static instance? : Falsity
private constructor(){
super()
}
static create() : Falsity{
if(Falsity.instance === undefined)
return Falsity.instance = new Falsity()
return Falsity.instance
}
override acceptVisitor<T>(visitor: FormulaVisitor<T>): T {
return visitor.visitFalsity(this)
}
}