Skip to content

Commit 493e917

Browse files
authored
#33 - Новый способ вывода +semver:feature (#125)
* #33 - лексика * #33 - грамматика * #33 - статический анализ * #33 - кодогенерация * #33 - удаление метода 'print' * #33 - правка скриптов в тестах * #33 - документация
1 parent ace888d commit 493e917

File tree

38 files changed

+140
-128
lines changed

38 files changed

+140
-128
lines changed

Readme.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,12 @@ let l = array[2]
168168
```
169169
let s = v2d as string
170170
```
171-
#### Стандартная библиотека
172-
- Функция `print` c сигнатурой `(string) => void`; осуществляет печать строки на экран
171+
#### Печать на экран
172+
```
173+
let obj = {}
174+
>>>obj
175+
>>>"Hello, World!"
176+
```
173177

174178
### Требования
175179

samples/abs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ function abs(x: number): number {
55
}
66

77
let x = -10
8-
let a = abs(x)
9-
print(a as string)
8+
>>> abs(x)

samples/arraddremove.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
let arr = [3,3,2,1,] ++ [4,5,]
22
arr::0
3-
print(arr as string)
3+
>>>arr

samples/arreditread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ let arr = [1,2,1,]
22
let item = arr[0]
33
item = item + 2
44
arr[0] = item
5-
print(arr as string)
5+
>>>arr

samples/ceil.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ function ceil(x: number): number {
22
return x + (1 - x % 1)
33
}
44

5-
let c = ceil(3.3)
6-
print(c as string)
5+
>>> ceil(3.3)

samples/cycled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ let obj: CycledType = {
55
x: null;
66
}
77
obj.x = obj
8-
print(obj as string)
8+
>>>obj

samples/defaultarray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ type numbers = number[]
22

33
let a: numbers
44

5-
print(a as string)
5+
>>>a

samples/equals.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ let obj2: withEquals = {
1414
prop: 2;
1515
}
1616

17-
let res = obj1.equals(obj2)
18-
print(res as string)
17+
>>> obj1.equals(obj2)

samples/exprtest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
let a = 1 + 3 * 2 - 5 / 5
22
// a == 6
3-
print(a as string)
3+
>>>a

samples/fastpow.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ function fastPow(base: number, power: number): number {
1717
return multiplier * multiplier * base
1818
}
1919

20-
let p = fastPow(13, 3)
21-
22-
print(p as string)
20+
>>> fastPow(13, 3)

samples/forwardref.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ let a: A = {
1212
};
1313
}
1414

15-
print(a as string)
15+
>>>a

samples/gcd.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ function gcd(a: number, b: number): number {
99
return gcd(bb, aa % bb)
1010
}
1111

12-
let g = gcd(1280, 720)
13-
14-
print(g as string)
12+
>>> gcd(1280, 720)

samples/lcm.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ function lcm(a: number, b: number): number {
1515
return (abs(a * b)) / (gcd(a, b))
1616
}
1717

18-
let l = lcm(1280, 720)
19-
print(l as string)
18+
>>> lcm(1280, 720)

samples/linkedlist.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ linkedList.append(3)
4242
linkedList.append(5)
4343
linkedList.append(7)
4444

45-
print(linkedList as string)
45+
>>>linkedList
4646

47-
let odds = linkedList.getOdd()
48-
print(odds as string)
47+
>>> linkedList.getOdd()

samples/objeditread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ let obj2 = {
1111
}
1212

1313
obj2.k = obj1.p.s.v
14-
print(obj2 as string)
14+
>>>obj2

samples/posneg.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ function negative(x: number): boolean {
88

99
let x = 0
1010
if (positive(x)) {
11-
print("positive")
11+
>>>"positive"
1212
} else if (negative(x)) {
13-
print("negative")
13+
>>>"negative"
1414
} else {
15-
print("zero")
15+
>>>"zero"
1616
}

samples/prime.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ function prime(x: number): boolean {
1414
return true
1515
}
1616

17-
let b = prime(13)
18-
19-
print(b as string)
17+
>>> prime(13)

samples/primefactor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ let factor = 2
55
while (factor * factor <= n) {
66
while (n % factor == 0) {
77
n = n / factor
8-
print(factor as string)
8+
>>>factor
99
}
1010

1111
factor = factor + 1
1212
}
1313

1414
if (n != 1) {
15-
print(factor as string)
15+
>>>factor
1616
}

samples/quicksort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ function quickSort(values: ints) {
4141

4242
let numbers = [6,2,4,3,7,1,5,]
4343
quickSort(numbers)
44-
print(numbers as string)
44+
>>>numbers

samples/range.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ function range(n: int): ints {
1212
return r
1313
}
1414

15-
let r1to6 = range(6)
16-
print(r1to6 as string)
15+
>>> range(6)

samples/recur.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ let n: number
2020
n = 6
2121
let fa6 = fact(6)
2222
let fa6s = fa6 as string
23-
print("fact(6) = " + fa6s)
23+
>>> "fact(6) = " + fa6s
2424

2525
n = 9
2626
let fi9 = fib(9)
2727
let fi9s = fi9 as string
28-
print("fib(9) = " + fi9s)
28+
>>> "fib(9) = " + fi9s

samples/scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ x = {
77
x: 1;
88
}
99

10-
print(x.x as string)
10+
>>>x.x

samples/searchinll.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function searchEven(n: node): number {
2626
let h = n
2727
while (h != null) {
2828
let d = h.data
29-
print(d as string)
29+
>>>d
3030
if (d % 2 == 0) {
3131
return d
3232
}
@@ -36,5 +36,5 @@ function searchEven(n: node): number {
3636
}
3737

3838
let j = searchEven(head)
39-
print("found Even:")
40-
print(j as string)
39+
>>>"found Even:"
40+
>>>j

samples/settable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type settable = {
55
function setprop(obj: settable, str: string) {
66
obj.prop = str
77
if (obj.prop == "1") {
8-
print("prop is one")
8+
>>>"prop is one"
99
}
1010
}
1111

@@ -15,4 +15,4 @@ let obj: settable = {
1515

1616
obj.setprop("1")
1717
obj.setprop("2")
18-
print(obj as string)
18+
>>>obj

samples/squareroot.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ function sqrt(num: number): number {
1818
return guess
1919
}
2020

21-
let sr = sqrt(3)
22-
23-
print(sr as string)
21+
>>> sqrt(3)

samples/summator.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ let summator: summable = {
1212
y: 2;
1313
}
1414

15-
let s = summator.sum()
16-
print(s as string)
15+
>>> summator.sum()

samples/tern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
let x = 1 > 0 ? 0 <= 1 ? 1 : 0 : -2 < 0 ? -1 : 0
2-
print(x as string)
2+
>>>x

samples/this.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type ObjType = {
55
}
66

77
function toString(obj: ObjType): string {
8-
let s = "object obj:\n"
8+
let s = "object obj: "
99
return s + (obj as string)
1010
}
1111

@@ -15,4 +15,4 @@ let obj: ObjType = {
1515
str: "field";
1616
}
1717

18-
print(obj.toString())
18+
>>> obj.toString()

samples/vec2d.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ function vlensquared(v: vec2d): number {
1818

1919
let v = makeVec(3, 4)
2020
let l = vlensquared(v)
21-
print(l as string)
21+
>>>l
2222
l = v.vlensquared()
23-
print(l as string)
23+
>>>l

src/Application/HydraScript.Application.CodeGeneration/Visitors/ExpressionInstructionProvider.cs

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -260,60 +260,47 @@ public AddressedInstructions Visit(CallExpression visitable)
260260
{
261261
if (visitable.IsEmptyCall)
262262
return [];
263+
263264
var methodCall = !visitable.Empty();
264-
if (visitable.Id.Name is "print" && !methodCall)
265+
string functionId;
266+
AddressedInstructions result = [];
267+
268+
if (methodCall)
265269
{
266-
var param = visitable.Parameters[0];
267-
268-
if (param is PrimaryExpression prim)
269-
return [new Print(_valueDtoConverter.Convert(prim.ToValueDto()))];
270-
271-
var result = param.Accept(This);
272-
var last = new Name(result.OfType<Simple>().Last().Left!);
273-
result.Add(new Print(last));
274-
275-
return result;
270+
var memberInstructions = visitable.Member.Accept(This);
271+
var lastMemberInstruction = (DotRead)memberInstructions[memberInstructions.End];
272+
memberInstructions.Remove(lastMemberInstruction);
273+
result.AddRange(memberInstructions);
274+
275+
functionId = lastMemberInstruction.Property;
276276
}
277277
else
278278
{
279-
string functionId;
280-
AddressedInstructions result = [];
281-
if (methodCall)
282-
{
283-
var memberInstructions = visitable.Member.Accept(This);
284-
var lastMemberInstruction = (DotRead)memberInstructions[memberInstructions.End];
285-
memberInstructions.Remove(lastMemberInstruction);
286-
result.AddRange(memberInstructions);
279+
functionId = visitable.Id;
280+
}
287281

288-
functionId = lastMemberInstruction.Property;
289-
}
282+
if (methodCall)
283+
{
284+
var caller = result.Any() ? result.OfType<Simple>().Last().Left! : visitable.Id;
285+
result.Add(new PushParameter(new Name(caller)));
286+
}
287+
288+
foreach (var expr in visitable.Parameters)
289+
{
290+
if (expr is PrimaryExpression primary)
291+
result.Add(new PushParameter(_valueDtoConverter.Convert(primary.ToValueDto())));
290292
else
291293
{
292-
functionId = visitable.Id;
294+
result.AddRange(expr.Accept(This));
295+
var id = result.OfType<Simple>().Last().Left!;
296+
result.Add(new PushParameter(new Name(id)));
293297
}
294-
var functionInfo = new FunctionInfo(functionId);
298+
}
295299

296-
if (methodCall)
297-
{
298-
var caller = result.Any() ? result.OfType<Simple>().Last().Left! : visitable.Id;
299-
result.Add(new PushParameter(new Name(caller)));
300-
}
301-
foreach (var expr in visitable.Parameters)
302-
{
303-
if (expr is PrimaryExpression primary)
304-
result.Add(new PushParameter(_valueDtoConverter.Convert(primary.ToValueDto())));
305-
else
306-
{
307-
result.AddRange(expr.Accept(This));
308-
var id = result.OfType<Simple>().Last().Left!;
309-
result.Add(new PushParameter(new Name(id)));
310-
}
311-
}
300+
result.Add(new CallFunction(
301+
new FunctionInfo(functionId),
302+
visitable.HasReturnValue));
312303

313-
result.Add(new CallFunction(
314-
functionInfo,
315-
visitable.HasReturnValue));
316-
return result;
317-
}
304+
return result;
318305
}
319306
}

0 commit comments

Comments
 (0)