Skip to content

Commit

Permalink
README 增加与别的编译器的比较
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Oct 21, 2024
1 parent cb994dd commit dc1773d
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 2 deletions.
21 changes: 20 additions & 1 deletion README-en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ without worrying about any performance
There is basically no error location information generated,
and the entire error mechanism is very poor However,
error reporting is not common. Usually,
we can still find the source of the error through a small amount of error information
we can still find the source of the error through a small amount of error information.

Fortunately, you won't encounter those terrifying advanced errors when using more basic functions

# How To Use
Let's first explain that the file name of this sample program is `mindustry_logic_bang_lang`,
Expand Down Expand Up @@ -277,3 +279,20 @@ Other compilation options can view their help without passing in any parameters:
```shell
mindustry_logic_bang_lang
```

# Comparison with other compilers
In addition to Bang's compiler, there are many useful compilers that can compile easy to write languages into `LogicLang`, such as:

- [mindcode](https://github.com/cardillan/mindcode)
- [mlogjs](https://github.com/mlogjs/mlogjs)

A simple example for comparison

1. **Bang**:
[code-and-compiled](./examples/pascals_triangle.mdtlbl)
2. **mlogjs**:
[code](https://github.com/mlogjs/mlogjs/blob/e17c84769a14c59ae0607db3c71db31d52ea8ad8/compiler/test/examples/pascals_triangle.js)
[compiled](https://github.com/mlogjs/mlogjs/blob/e17c84769a14c59ae0607db3c71db31d52ea8ad8/compiler/test/examples/pascals_triangle.mlog)
3. **mindcode**:
[code](./examples/pascals_triangle.mnd)
*Currently not compiled*
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ cargo install --path . # 执行这个你可以在你的shell中直接使用它(
报错不怎么友好, 不过报错也比较少, 信息也差不多够找出错误\
~~就是可能使用高级功能时调试起来过于地狱~~

不过好在, 你使用较为基本的功能并不会遇到那些恐怖的高级错误

# 如何使用
我们先说明本示例程序的文件名为`mindustry_logic_bang_lang`,
因为可能由于平台原因或个人进行的重命名带来名称不同,
Expand Down Expand Up @@ -237,3 +239,21 @@ mindustry_logic_bang_lang cl < my_source.mdtlbl > out.logic
```shell
mindustry_logic_bang_lang
```

# 关于其它编译器的对比
除了 bang 的编译器, 还有不少好用的编译器可以将易于编写的语言编译到`逻辑语言`,
例如:

- [mindcode](https://github.com/cardillan/mindcode)
- [mlogjs](https://github.com/mlogjs/mlogjs)

一个简单的用于对比的例子

1. **Bang**:
[code-and-compiled](./examples/pascals_triangle.mdtlbl)
2. **mlogjs**:
[code](https://github.com/mlogjs/mlogjs/blob/e17c84769a14c59ae0607db3c71db31d52ea8ad8/compiler/test/examples/pascals_triangle.js)
[compiled](https://github.com/mlogjs/mlogjs/blob/e17c84769a14c59ae0607db3c71db31d52ea8ad8/compiler/test/examples/pascals_triangle.mlog)
3. **mindcode**:
[code](./examples/pascals_triangle.mnd)
*目前暂未编译*
128 changes: 128 additions & 0 deletions examples/pascals_triangle.mdtlbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# pre define macros
const ForC = (const match @ {
#**
* Static expand for loop impl
*#
*Stop F {
take ForC[0 Stop F];
}
*Start *Stop F {
take ForC[Start Stop 1 F];
}
*Start *Stop *Step F {
const Do = (const match @ {
I:[?_0 < Stop] {
take F[I];
take Do[(?I+Step)->$];
}
});
take Do[Start];
}
});
const ForD = (const match @ {
#**
* Dynamic for loop
*#
*Stop F {
take ForD[0 Stop F];
}
*Start *Stop F {
take ForD[Start Stop 1 F];
}
*Start *Stop *Step F {
# Dynamic Running impl
take+I(?I:Start);

while I < Stop {
take F[I];
I += Step;
}
}
});
const MakeMemMethods = (inline@ *Mem {
const Mem.Read = (match @ {
Addr {
read $ .. Addr;
}
Result Addr {
setres Result;
read Result .. Addr;
}
});
const Mem.Write = (setres ..; match @ {
@ Value {
inline@ Addr {
write Value .. Addr;
}
}
});
});

# codes
const TriangleSize = 10;
const CurLine = cell1;
const PrevLine = cell2;
take MakeMemMethods[CurLine PrevLine];

print"\
1
1 1
";

lastSize = 2;

take PrevLine.Write[0 1 1]; # like `PrevLine[0, 1] = 1`

take ForD[3 (%(*TriangleSize+1))->$ const(match @ { I {
take CurLine.Write[0 1];

take ForD[1 lastSize const(match @ { J {
take CurLine.Write[J (*PrevLine.Read[(*J- 1)] + PrevLine.Read[J])];
}})];
take CurLine.Write[lastSize 1];
lastSize++;

take ForD[lastSize const(match @ { C {
print CurLine.Read[cur_c C];
take PrevLine.Write[C cur_c];
if C < (*lastSize- 1) { print " "; }
}})];
print "\n";
}})];

printflush message1;

#* >>>
print "1\n1 1\n"
set lastSize 2
write 1 cell2 0
write 1 cell2 1
set __26 3
jump 31 greaterThanEq __26 11
write 1 cell1 0
set __43 1
jump 16 greaterThanEq __43 lastSize
op sub __52 __43 1
read __51 cell2 __52
read __54 cell2 __43
op add __49 __51 __54
write __49 cell1 __43
op add __43 __43 1
jump 9 lessThan __43 lastSize
write 1 cell1 lastSize
op add lastSize lastSize 1
set __74 0
jump 28 greaterThanEq __74 lastSize
read cur_c cell1 __74
print cur_c
write cur_c cell2 __74
op sub __85 lastSize 1
jump 26 greaterThanEq __74 __85
print " "
op add __74 __74 1
jump 20 lessThan __74 lastSize
print "\n"
op add __26 __26 1
jump 6 lessThan __26 11
printflush message1
*#
38 changes: 38 additions & 0 deletions examples/pascals_triangle.mnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#**
* This is code in the Mindcode language
*
* Write this code to compare the differences between various compiler styles.
* As I am a temporary learning Mindcode,
* You can create a PR to fix errors or make it more concise
*#

const TRIANGLE_SIZE = 10;
currentLine = cell1;
previousLine = cell2;

print("1\n1 1\n");

lastSize = 2;

previousLine[0] = 1;
previousLine[1] = 1;

for i in 3...TRIANGLE_SIZE do
currentLine[0] = 1
for j in 1..lastSize do
currentLine[j] = previousLine[j - 1] + previousLine[j];
end;
currentLine[lastSize] = 1;
lastSize += 1;

for c in 0..lastSize do
print(currentLine[c]);
previousLine[c] = currentLine[c];
if c < lastSize - 1 then
print(" ");
end;
end;
print("\n");
end;

printflush(message1);
2 changes: 1 addition & 1 deletion examples/std/match_utils.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ const MatchUtils.ForC = ([MU:MatchUtils](const match @ {
}
});
take Do[Start];
}
}
}));

0 comments on commit dc1773d

Please sign in to comment.