Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rust backend #17

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion awib-skeleton.b
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// T=6: lang_ruby
// T=5: (null)
// T=4: lang_go
// T=3: (null)
// T=3: lang_rust

#include(lang_generic/backend.b)

Expand Down
9 changes: 4 additions & 5 deletions frontend/frontend.b
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@
<[-
# build target string for backend index 3
% T X 0 0 0 (target) 0 s 3 *0 0
>>+++++++[->+++++++<]++++++++++>
[->++>++>++>++>++>++>++>++>++>++<<<<<<<<<<]>
++++++++++>->++++++++++++>+++++>--->++>+++++++++++++++++++>+++++++++++>
+++++++++++>+++++++++++++++++++++++<<<<<<<<<<<<<
% T X 0 0 0 (target) 0 s 3 *0 0 10 0 "lang_dummy" 0
>+++++++++[->+>++++++++++++<<]>>[->+>+>+>+>+>+>+>+>+<<<<<<<<<]
>>----------->++>----->------------->++++++>+++++++++>+++++++>++++++++
<<<<<<<<<<<<
% T X 0 0 0 (target) 0 s 3 *0 0 9 0 "lang_rust" 0
]>]

<[-
Expand Down
66 changes: 66 additions & 0 deletions lang_generic/DEVEL
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,69 @@ Postcondition is:
| | run stdin stdout |
+--------------+------------------------------------------------------------+
Table 5 - Language definition for Tcl

### Rust

+--------------+------------------------------------------------------------+
| Code block | Output |
+--------------+------------------------------------------------------------+
| code_ADD | buf[p] = buf[p].wrapping_add( |
+--------------+------------------------------------------------------------+
| code_INPUT | i.read(&mut buf[p..p+1])?; |
+--------------+------------------------------------------------------------+
| code_SUB | buf[p] = buf[p].wrapping_sub( |
+--------------+------------------------------------------------------------+
| code_OUTPUT | o.write(&buf[p..p+1])?; |
+--------------+------------------------------------------------------------+
| code_LEFT | p -= |
+--------------+------------------------------------------------------------+
| code_RIGHT | p += |
+--------------+------------------------------------------------------------+
| code_OPEN | while buf[p] != 0 { |
+--------------+------------------------------------------------------------+
| code_CLOSE | } |
+--------------+------------------------------------------------------------+
| code_SET | buf[p] = |
+--------------+------------------------------------------------------------+
| code_LMUL1 | let t = p - |
+--------------+------------------------------------------------------------+
| code_LMUL2 | ; buf[t] = buf[t].wrapping_add(buf[p].wrapping_mul( |
+--------------+------------------------------------------------------------+
| code_RMUL1 | let t = p + |
+--------------+------------------------------------------------------------+
| code_RMUL2 | ; buf[t] = buf[t].wrapping_add(buf[p].wrapping_mul( |
+--------------+------------------------------------------------------------+
| post_ADD | ); |
+--------------+------------------------------------------------------------+
| post_SUB | ); |
+--------------+------------------------------------------------------------+
| post_LEFT | ; |
+--------------+------------------------------------------------------------+
| post_RIGHT | ; |
+--------------+------------------------------------------------------------+
| post_SET | ; |
+--------------+------------------------------------------------------------+
| post_LMUL2 | )); |
+--------------+------------------------------------------------------------+
| post_RMUL2 | )); |
+--------------+------------------------------------------------------------+
| header | use std::io::{self, Read, Write}; |
| | |
| | #[allow(unused_variables)] |
| | #[allow(unused_mut)] |
| | fn run<R, W>(mut i: R, mut o: W) -> io::Result<()> |
| | where |
| | R: Read, |
| | W: Write, |
| | { |
| | let mut p = 127; |
| | let mut buf = [0u8; 0x10100]; |
+--------------+------------------------------------------------------------+
| footer | Ok(()) |
| | } |
| | |
| | fn main() -> io::Result<()> { |
| | return run(std::io::stdin(), std::io::stdout()); |
| | } |
+--------------+------------------------------------------------------------+
Table 5 - Language definition for Rust
7 changes: 5 additions & 2 deletions lang_generic/backend.b
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# run language definition according to T with S=P
% 1 *T T D d D d 7(0) P P 0 Q (code)

---[-[-[-[-[<->++++[-]]

<[->>>>>
Expand All @@ -43,8 +44,10 @@
<<<<<]>]

<[->>>>>
# T=3: (null)
# A dummy backend lived here once upon a time
# T=3: lang_rust
% (stuff) *D d 7(0) S (stuff)
#include(rust.b)

<<<<<]

>>>>>>>>>>>>>>>>
Expand Down
232 changes: 232 additions & 0 deletions lang_generic/rust.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
% (stuff) *D d 7(0) S (stuff)
% where integer S indicates which code block to output
% 16 b integer Dd is loop depth

# indentation if S le 13: 2*add(D*256 d) blank spaces
>>>>>>>+>>
-[-[-[-[-[-[-[-[-[-[-[-[-[<<->>[-<+>]]
<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+>]<+<
% D d 0 0 0 0 0 *c S 0 (where c == S le 13 ? 1 : 0)
[-<<<<++++[-<++++++++>]<....<[->....<]
<[->++++++++++++++++[->................................
................................<]<]>>[-]>>>>>]
<<<<<<<[-]>[-]>>>>>>
% (stuff) 7(0) *0 S 0 (stuff)

+>-[-[-[-[-[-[-[-[-[-[-[-[-[--------[--[--[-[---[--[--[---[-
[<->++++++++++++++++++++++++++++++++++++++[-]]

<[-
# footer
<<<<<<<+++++++++++++++++++++[->++++>++++
++>++>++>+++++>+++++<<<<<<]>>>----------
....<<-----.>>>>++.<--..+..<<<<+++++++++
+.>>-.<<..>>>>>>---.<+++.<<.>>-.>-----.<
----.+++++.<-.+.<.>++++.++++++++++++++++
+.<.>>-----.++++++.<----..<<<+++.>>>>>++
++.<++++.++.>+++++++.<-.<++.<++++++++.+.
>++.<---------.<--.<<.>>>....>>--.>-----
--.<++.+.---.----.<<.>>++++.+++.-------.
<<++++++++.>>+++++.+.>-.<<----..>>+++++.
<-----.<..>++++.+.>-----.+++++.+++++.<<<
.+.+++.------------.>>-.+.>----------.<<
..>>+++++.<-----.<..>++++.+.>-----.<----
-.++++++.-.<<++++++++.+..>+.<<<<.>>++.<<
.[<]>[[-]>]
]>]

<[-
# header
<<<<<<<+++++++++++++++++++[->++>++>++++>
+++>++++++>+++++<<<<<<]>>>>>+++.--.>++++
++.<<<<------.>>>.+.>-.<<+..>>+++++.<---
--.<..>++++++++++++.--------.>----.+++++
++.------.<<<<<++++++.>.>++++++.>>>-.---
-.+++.<<<<<.>.>+++++.>>-.>+++++.<++.>---
-.<+++++++++.<+.<<<<++++++++++..>>+++.>+
+++.++++++.>>>+++++++..+++.<------.<<<<-
---.>>>>--.>-.<.--.<<++++.-.-----.>>+++.
<<++.>>----.>-----.<<<.+.>>>+++.<<<+++.>
>+.<<<<+.>>--------.<<<.>>.>--.++++++.>>
>..+++.<++++.<<<<-.>>>>--.>-.<.--.<<++++
.-.-----.>>>-.<++.-.<<<<+.>>--.<<<.>>>++
+++++++.>>>+.<<<<---.>>>--.+++.>.<<+.<--
------------------.<<+++.>.>+++++.>++.<<
<----.>>>>>-.<.-.<<<.>>>>----.<<----.<<.
>-----.<<++++.>.>>>>++++.<+.-.<<<.>>>>++
.<<.<<.>+++++.<<---.>.<++++.>>>++++.<<.>
>>>------.<-----.<----..<-----.>>>----.<
++++.++.>+++++++.<-.<++.<<<-----.+.>>>++
.<<<<.>>>>>+++.>----.---.<-----.>.<<<<<<
.>>....>.>----.<<.>.>>>.----.+++.<<<<<++
+.<.>>....>+++++.>.<<.>.>>.>+++++.<++.>-
---.<<<<<.<.>>>>>+++++++.<<<<<.>>....>>>
>+++++++.-------.<-------.<<<.>>>-------
.++++++++.-.<<<.>>>----.<<<.>>+++.<<.<++
+++.+.+++++.++++.<.>>....>>>----.>.<++++
++++.<<<.>>>-------.++++++++.-.<<<.>>>>-
--.<+.>++++.<<<<.>>.<<.>++++.<<---------
--.>>>>.<-----.+++.<<.<.>>>>+++.<<<<+.-.
+.-..>>++.>.<<<<.[[-]>]
]>]

<[-
# post_RMUL2
++++++[->+++++++<]>-..>+++[-<++++++>]<.
+[------<+>]<.[-]
]>]

<[-
# post_LMUL2
++++++[->+++++++<]>-..>+++[-<++++++>]<.
+[------<+>]<.[-]
]>]

<[-
# post_SET
++++++++++[->++++++<]>-.+[------<+>]<.[-]
]>]

<[-
# post_RIGHT
++++++++++[->++++++<]>-.+[------<+>]<.[-]
]>]

<[-
# post_LEFT
++++++++++[->++++++<]>-.+[------<+>]<.[-]
]>]

<[-
# post_SUB
++++++[->+++++++<]>-.>+++[-<++++++>]<.+
[------<+>]<.[-]
]>]

<[-
# post_ADD
++++++[->+++++++<]>-.>+++[-<++++++>]<.+
[------<+>]<.[-]
]>]

<[-
# code_RMUL2
<<<<<<+++++++++++++++++++++++++[->++>+>
++++>+++++>++++<<<<<]>+++++++++.>+++++++
.>--.>--------.>++.<<-------.>-.<++.<.<+
+.>.>+++++.>+.>.<<-------.>-.<++.<++++++
++++++++.>>+++.-----.<++++.>--..>+++.<--
.>--.<<--.++.+++..<------.>--.>+++++++.>
-.<<-------.>-----.<++.<++++++.>>+++++++
.-----.<++++.>--..>+++.<--.>--.<<--.>-.+
+++++++.>+++++.<<<------.<[[-]>]
]>]

<[-
# code_RMUL1
<<<<<<++++++++++++++++++++[->++>+++>++>+
++++>+++++<<<<<]>>>>++++++++.>+.<+++++++
+.<--------.>.<.<+.>.>----.<.<<+++.
[[-]>]
]>]

<[-
# code_LMUL2
<<<<<<+++++++++++++++++++++++++[->++>+>
++++>+++++>++++<<<<<]>+++++++++.>+++++++
.>--.>--------.>++.<<-------.>-.<++.<.<+
+.>.>+++++.>+.>.<<-------.>-.<++.<++++++
++++++++.>>+++.-----.<++++.>--..>+++.<--
.>--.<<--.++.+++..<------.>--.>+++++++.>
-.<<-------.>-----.<++.<++++++.>>+++++++
.-----.<++++.>--..>+++.<--.>--.<<--.>-.+
+++++++.>+++++.<<<------.<
[[-]>]
]>]

<[-
# code_LMUL1
<<<<<<++++++++++++++++++++++[->+++++>++>
+++++>+>+++<<<<<]>--.-------.>>++++++.>+
+++++++++.<.>.>-----.<.<----.>.<<+.<
[[-]>]
]>]

<[-
# code_SET
<<<<<++++++++++++++++++++++++++++++[->++
>+>+++>++++<<<<]>>>++++++++.>---.<++++.-
----------.>-----.<++.<++.<+.>.<[[-]>]
]>]

<[-
# code_CLOSE
++++++++++[->++++++++++++<]>+++++.[-]+++
+++++++.[-]<
]>]

<[-
# code_OPEN
<<<<<+++++++++++++++++++++++++++++++[->+
+++>+++>+>++<<<<]>-----.>+++++++++++.+.+
++.-------.>+.<---.<--.>++++.-----------
.<-----.>++.>.+.>-.<-.>-------------.<.<
<+++++++++++.<++++++++++.[[-]>]
]>]

<[-
# code_RIGHT
<<<<<++++++++++++++++++++++++++++[->+>++
>++++>+<<<<]>>>.>++++.+++++++++++.<<++++
+.<++++.[[-]>]
]>]

<[-
# code_LEFT
<<<<<++++++++++++++++++++++++++++[->++++
>+>++>++<<<<]>.>++++.>-----------.>+++++
.<<.<[[-]>]
]>]

<[-
# code_OUTPUT
<<<<<<+++++++++++++++++++++[->+++>++>+++
++>+++++>++<<<<<]>>>++++++.<++++.>++++++
++.-----.>.<++.>----.>--.--.<---.<+.>+++
+.-----------.<-----.<..>.<---.++++++.>>
++.>+++.<<<<.----.<++++++++++.[[-]>]
]>]

<[-
# code_SUB
<<<<<++++++++++++++++++++++++++++++[->++
>+>+++>++++<<<<]>>>++++++++.>---.<++++.-
----------.>-----.<++.<++.<+.>.>+++++.>+
++++.<++++.-----------.>-----.<++.<+++++
+++++++++.>>+++++++.-----.<++++.>--..---
----.+++++.<++++++.--------.>+++++.++.<+
++.<------.<[[-]>]
]>]

<[-
# code_INPUT
<<<<<<+++++++++++++++++++++[->+++>++>+++
++>+++++>++<<<<<]>>>.<++++.>+++++++++.>-
---.----.+++.>--.--.<<-----.++++++++.-.>
>------.<--.<+.>++++.-----------.<-----.
<..>.<---.++++++.>>++.<<--------.<.----.
<++++++++++.[[-]>]
]>]

<[-
# code_ADD
<<<<<++++++++++++++++++++++++++++++[->++
++>+++>+>++<<<<]>>++++++++.<---.>++++.--
---------.<-----.>++.>++.>+.<.<+++++.<++
+++.>++++.-----------.<-----.>++.>++++++
++++++++.<<+++++++.-----.>++++.<--..----
---.+++++.-------.>--.++.+++..>------.<<
[[-]>]
]<<<<<<<

% (stuff) *0 9(0) (stuff)
24 changes: 24 additions & 0 deletions test/rust_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import subprocess
import os

import backend


class RustTest(backend.LangGenericTestCase):
BACKEND_INDEX = 3
MAX_NESTED_LOOPS = 95

def run_program(self, path, input):
os.rename(path, path + ".rs")
subprocess.call(["rustc", path + ".rs", "-o", path])
p = subprocess.Popen([path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
stdout, stderr = p.communicate(input)
self.assertEqual(p.returncode, 0)
return stdout


if __name__ == "__main__":
import unittest
unittest.main()
Loading