Skip to content

Commit 89591d9

Browse files
committed
Separate values from expressions
1 parent b0477d9 commit 89591d9

File tree

7 files changed

+165
-86
lines changed

7 files changed

+165
-86
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ module.exports = {
165165
'no-invalid-this': 'off',
166166
'no-loss-of-precision': 'off',
167167
'no-new-wrappers': 'error',
168-
'no-redeclare': 'error',
169168
'no-return-await': 'error',
170169
'no-shadow': 'off',
171170
'no-throw-literal': 'error',

src/cmd_line/commands/let.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
modulo,
1212
multiply,
1313
subtract,
14+
toExpr,
1415
} from '../../vimscript/expression/build';
1516
import { EvaluationContext, toInt, toString } from '../../vimscript/expression/evaluate';
1617
import {
@@ -179,19 +180,19 @@ export class LetCommand extends ExCommand {
179180
const value = context.evaluate(this.args.expression);
180181
const newValue = (_var: Expression, _value: Value) => {
181182
if (this.args.operation === '+=') {
182-
return context.evaluate(add(_var, _value));
183+
return context.evaluate(add(_var, toExpr(_value)));
183184
} else if (this.args.operation === '-=') {
184-
return context.evaluate(subtract(_var, _value));
185+
return context.evaluate(subtract(_var, toExpr(_value)));
185186
} else if (this.args.operation === '*=') {
186-
return context.evaluate(multiply(_var, _value));
187+
return context.evaluate(multiply(_var, toExpr(_value)));
187188
} else if (this.args.operation === '/=') {
188-
return context.evaluate(divide(_var, _value));
189+
return context.evaluate(divide(_var, toExpr(_value)));
189190
} else if (this.args.operation === '%=') {
190-
return context.evaluate(modulo(_var, _value));
191+
return context.evaluate(modulo(_var, toExpr(_value)));
191192
} else if (this.args.operation === '.=') {
192-
return context.evaluate(concat(_var, _value));
193+
return context.evaluate(concat(_var, toExpr(_value)));
193194
} else if (this.args.operation === '..=') {
194-
return context.evaluate(concat(_var, _value));
195+
return context.evaluate(concat(_var, toExpr(_value)));
195196
}
196197
return _value;
197198
};
@@ -233,7 +234,7 @@ export class LetCommand extends ExCommand {
233234
);
234235
varValue.items[idx] = newItem;
235236
context.setVariable(variable.variable, varValue, this.args.lock);
236-
} else if (varValue.type === 'dict_val') {
237+
} else if (varValue.type === 'dictionary') {
237238
const key = toString(context.evaluate(variable.index));
238239
const newItem = newValue(
239240
{

src/vimscript/expression/build.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import {
1717
Value,
1818
BlobValue,
1919
FuncrefCallExpression,
20+
NumberExpression,
21+
BlobExpression,
22+
DictionaryExpression,
23+
FloatExpression,
24+
FuncRefExpression,
25+
StringExpression,
2026
} from './types';
2127

2228
export function int(value: number): NumberValue {
@@ -53,17 +59,20 @@ export function list(items: Value[]): ListValue {
5359

5460
export function dictionary(items: Map<string, Value>): DictionaryValue {
5561
return {
56-
type: 'dict_val',
62+
type: 'dictionary',
5763
items,
5864
};
5965
}
6066

61-
export function funcref(name: string, arglist?: ListValue, dict?: DictionaryValue): FuncRefValue {
67+
export function funcref(args: {
68+
name: string;
69+
body?: (args: Value[]) => Value;
70+
arglist?: ListValue;
71+
dict?: DictionaryValue;
72+
}): FuncRefValue {
6273
return {
6374
type: 'funcref',
64-
name,
65-
arglist,
66-
dict,
75+
...args,
6776
};
6877
}
6978

@@ -74,6 +83,42 @@ export function blob(data: Uint8Array<ArrayBuffer>): BlobValue {
7483
};
7584
}
7685

86+
export function toExpr(value: NumberValue): NumberExpression;
87+
export function toExpr(value: FloatValue): FloatExpression;
88+
export function toExpr(value: StringValue): StringExpression;
89+
export function toExpr(value: ListValue): ListExpression;
90+
export function toExpr(value: DictionaryValue): DictionaryExpression;
91+
export function toExpr(value: FuncRefValue): FuncRefExpression;
92+
export function toExpr(value: BlobValue): BlobExpression;
93+
export function toExpr(value: Value): Expression;
94+
export function toExpr(value: Value): Expression {
95+
if (value.type === 'number' || value.type === 'float' || value.type === 'string') {
96+
return value;
97+
} else if (value.type === 'list') {
98+
return listExpr(value.items.map(toExpr));
99+
} else if (value.type === 'dictionary') {
100+
return {
101+
type: 'dictionary',
102+
items: [...value.items.entries()].map(([key, val]) => [str(key), toExpr(val)]),
103+
};
104+
} else if (value.type === 'funcref') {
105+
return {
106+
type: 'funcref',
107+
name: value.name,
108+
body: value.body,
109+
arglist: value.arglist ? toExpr(value.arglist) : undefined,
110+
dict: value.dict ? toExpr(value.dict) : undefined,
111+
};
112+
} else if (value.type === 'blob') {
113+
return {
114+
type: 'blob',
115+
data: value.data,
116+
};
117+
}
118+
const guard: never = value;
119+
throw new Error(`Unknown value type in toExpr()`);
120+
}
121+
77122
export function listExpr(items: Expression[]): ListExpression {
78123
return {
79124
type: 'list',

src/vimscript/expression/displayValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function displayValue(value: Value, topLevel = true): string {
1616
return topLevel ? value.value : `'${value.value.replace("'", "''")}'`;
1717
case 'list':
1818
return `[${value.items.map((v) => displayValue(v, false)).join(', ')}]`;
19-
case 'dict_val':
19+
case 'dictionary':
2020
return `{${[...value.items]
2121
.map(([k, v]) => `'${k}': ${displayValue(v, false)}`)
2222
.join(', ')}}`;

0 commit comments

Comments
 (0)