@@ -2,28 +2,20 @@ export enum OperationTypes {
22 Arithmetic , Assignment , Comparison , Logical , Membership
33} ;
44
5- export type Operators =
6- "=" | "+=" | "-=" | "*=" | "/=" | "++" | "--"
7- | "+" | "-" | "*" | "/" | "%" | "**" | "//"
8- | ">" | ">=" | "==" | "!=" | "<>" | "<" | "<="
9- | "and" | "or" | "not"
10- | "in" | "not in" ;
5+ export type AssignmentOperators = "=" | "+=" | "-=" | "*=" | "/=" | "++" | "--" ;
6+ export type ArithmeticOperators = "+" | "-" | "*" | "/" | "%" | "**" | "//" ;
7+ export type ComparisonOperators = ">" | ">=" | "==" | "!=" | "<>" | "<" | "<=" ;
8+ export type LogicalOperators = "and" | "or" | "not" | "not in" ;
9+ export type MembershipOperators = "in" ;
1110
11+ export type Operators = AssignmentOperators | ArithmeticOperators | ComparisonOperators | LogicalOperators | MembershipOperators ;
1212
1313export const OperatorsMap : Record < Operators , OperationTypes > = {
14- "=" : OperationTypes . Assignment ,
15- "+=" : OperationTypes . Assignment ,
16- "-=" : OperationTypes . Assignment ,
17- "*=" : OperationTypes . Assignment ,
18- "/=" : OperationTypes . Assignment ,
19- "++" : OperationTypes . Assignment ,
20- "--" : OperationTypes . Assignment ,
21-
22- "+" : OperationTypes . Arithmetic ,
23- "-" : OperationTypes . Arithmetic ,
24- "*" : OperationTypes . Arithmetic ,
25- "/" : OperationTypes . Arithmetic ,
26- "%" : OperationTypes . Arithmetic ,
14+ "+" : OperationTypes . Arithmetic ,
15+ "-" : OperationTypes . Arithmetic ,
16+ "*" : OperationTypes . Arithmetic ,
17+ "/" : OperationTypes . Arithmetic ,
18+ "%" : OperationTypes . Arithmetic ,
2719 "**" : OperationTypes . Arithmetic ,
2820 "//" : OperationTypes . Arithmetic ,
2921
@@ -32,13 +24,126 @@ export const OperatorsMap: Record<Operators, OperationTypes> = {
3224 "==" : OperationTypes . Comparison ,
3325 "!=" : OperationTypes . Comparison ,
3426 "<>" : OperationTypes . Comparison ,
35- "<" : OperationTypes . Comparison ,
27+ "<" : OperationTypes . Comparison ,
3628 "<=" : OperationTypes . Comparison ,
3729
3830 "and" : OperationTypes . Logical ,
39- "or" : OperationTypes . Logical ,
31+ "or" : OperationTypes . Logical ,
4032 "not" : OperationTypes . Logical ,
33+ "not in" : OperationTypes . Logical ,
4134
42- "in" : OperationTypes . Membership ,
43- "not in" : OperationTypes . Logical
35+ "in" : OperationTypes . Membership ,
36+
37+ "=" : OperationTypes . Assignment ,
38+ "+=" : OperationTypes . Assignment ,
39+ "-=" : OperationTypes . Assignment ,
40+ "*=" : OperationTypes . Assignment ,
41+ "/=" : OperationTypes . Assignment ,
42+ "++" : OperationTypes . Assignment ,
43+ "--" : OperationTypes . Assignment ,
4444} ;
45+
46+ export type Primitive = string | number | boolean | null ;
47+
48+ export type ExpressionOperators = ArithmeticOperators | ComparisonOperators | LogicalOperators | MembershipOperators ;
49+ type ExpressionOperation = ( l : Primitive , r : Primitive ) => Primitive ;
50+
51+ export const OperationFuncs : Record < ExpressionOperators , ExpressionOperation > = {
52+ "+" : ( l , r ) => arithmeticOperation ( l , r , "+" ) ,
53+ "-" : ( l , r ) => arithmeticOperation ( l , r , "-" ) ,
54+ "/" : ( l , r ) => arithmeticOperation ( l , r , "/" ) ,
55+ "*" : ( l , r ) => arithmeticOperation ( l , r , "*" ) ,
56+ "%" : ( l , r ) => arithmeticOperation ( l , r , "%" ) ,
57+ "**" : ( l , r ) => arithmeticOperation ( l , r , "**" ) ,
58+ "//" : ( l , r ) => arithmeticOperation ( l , r , "//" ) ,
59+
60+ ">" : ( l , r ) => comparissonOperation ( l , r , ">" ) ,
61+ ">=" : ( l , r ) => comparissonOperation ( l , r , ">=" ) ,
62+ "<" : ( l , r ) => comparissonOperation ( l , r , "<" ) ,
63+ "<=" : ( l , r ) => comparissonOperation ( l , r , "<=" ) ,
64+ "==" : ( l , r ) => comparissonOperation ( l , r , "==" ) ,
65+ "!=" : ( l , r ) => comparissonOperation ( l , r , "!=" ) ,
66+ "<>" : ( l , r ) => comparissonOperation ( l , r , "<>" ) ,
67+
68+ "and" : ( l , r ) => logicalOperation ( l , r , "and" ) ,
69+ "or" : ( l , r ) => logicalOperation ( l , r , "or" ) ,
70+ "not" : ( l , r ) => logicalOperation ( l , r , "not" ) ,
71+ "not in" : ( l , r ) => logicalOperation ( l , r , "not in" ) ,
72+
73+ "in" : ( l , r ) => membershipOperation ( l , r , "in" )
74+ }
75+
76+ function membershipOperation ( l : Primitive , r : Primitive , op : MembershipOperators ) : Primitive {
77+ if ( typeof l === 'string' ) {
78+ return ( l as string ) . includes ( String ( r ) ) ;
79+ }
80+
81+ if ( Array . isArray ( l ) ) {
82+ return ( l as any [ ] ) . includes ( r ) ;
83+ }
84+
85+ throw new Error ( `Unknown operation '${ op } '` ) ;
86+ }
87+
88+ function logicalOperation ( l : Primitive , r : Primitive , op : LogicalOperators ) : Primitive {
89+ switch ( op ) {
90+ case 'and' :
91+ return l as any && r as any ;
92+
93+ case 'or' :
94+ return ( l as any ) || ( r as any ) ;
95+ }
96+ throw new Error ( `Unknown operation '${ op } '` ) ;
97+ }
98+
99+ function comparissonOperation ( l : Primitive , r : Primitive , op : ComparisonOperators ) : Primitive {
100+ switch ( op ) {
101+ case '==' :
102+ return l as any === r as any ;
103+
104+ case '!=' :
105+ return ( l as any ) !== ( r as any ) ;
106+
107+ case '<>' :
108+ return ( l as any ) !== ( r as any ) ;
109+
110+ case '>' :
111+ return ( l as number ) > ( r as number ) ;
112+
113+ case '<' :
114+ return ( l as number ) < ( r as number ) ;
115+
116+ case '>=' :
117+ return ( l as number ) >= ( r as number ) ;
118+
119+ case '<=' :
120+ return ( l as number ) <= ( r as number ) ;
121+ }
122+
123+ throw new Error ( `Unknown operation '${ op } '` ) ;
124+ }
125+
126+ function arithmeticOperation ( l : Primitive , r : Primitive , op : ArithmeticOperators ) : Primitive {
127+
128+ switch ( op ) {
129+ case '+' :
130+ return l as any + r as any ;
131+
132+ case '-' :
133+ return ( l as any ) - ( r as any ) ;
134+
135+ case '*' :
136+ return ( l as number ) * ( r as number ) ;
137+
138+ case '/' :
139+ return ( l as number ) / ( r as number ) ;
140+
141+ case '%' :
142+ return ( l as number ) % ( r as number ) ;
143+
144+ case '**' :
145+ return Math . pow ( l as number , r as number ) ;
146+ }
147+
148+ throw new Error ( `Unknown operation '${ op } '` ) ;
149+ }
0 commit comments