Compare commits
No commits in common. "master" and "v1.0.4" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
/archive
|
||||||
/bin
|
/bin
|
0
.gitmodules
vendored
0
.gitmodules
vendored
85
Cargo.lock
generated
85
Cargo.lock
generated
@ -2,12 +2,6 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cfg-if"
|
|
||||||
version = "1.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@ -16,91 +10,14 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "labast"
|
name = "labast"
|
||||||
version = "1.0.6"
|
version = "1.0.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fastrand",
|
"fastrand",
|
||||||
"libloading",
|
|
||||||
"pico-args",
|
"pico-args",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libloading"
|
|
||||||
version = "0.8.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"windows-sys",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pico-args"
|
name = "pico-args"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows-sys"
|
|
||||||
version = "0.48.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
|
||||||
dependencies = [
|
|
||||||
"windows-targets",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows-targets"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
|
||||||
dependencies = [
|
|
||||||
"windows_aarch64_gnullvm",
|
|
||||||
"windows_aarch64_msvc",
|
|
||||||
"windows_i686_gnu",
|
|
||||||
"windows_i686_msvc",
|
|
||||||
"windows_x86_64_gnu",
|
|
||||||
"windows_x86_64_gnullvm",
|
|
||||||
"windows_x86_64_msvc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_aarch64_gnullvm"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_aarch64_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_i686_gnu"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_i686_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_gnu"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_gnullvm"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "labast"
|
name = "labast"
|
||||||
version = "1.0.6"
|
version = "1.0.4"
|
||||||
authors = [ "n3tael" ]
|
authors = [ "n3tael" ]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A Labaski interpreter written in Rust."
|
description = "A zero-dependencies Labaski interpreter written in Rust."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "apache-2.0"
|
license = "MIT"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
@ -18,5 +18,4 @@ panic = "abort"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fastrand = "2.0.1"
|
fastrand = "2.0.1"
|
||||||
libloading = "0.8.1"
|
|
||||||
pico-args = "0.5.0"
|
pico-args = "0.5.0"
|
||||||
|
202
LICENSE
202
LICENSE
@ -1,202 +0,0 @@
|
|||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright 2024 n3tael
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 n3tael
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -1,3 +1,8 @@
|
|||||||
# Labast
|
# Labast
|
||||||
|
A Labaski interpreter written in Rust. Fully supports Labashki specs 1.12.
|
||||||
|
|
||||||
A Labaski interpreter written in Rust. Fully supports Labashki specs 1.15.1
|
## Examples
|
||||||
|
See `scripts` directory.
|
||||||
|
|
||||||
|
## Syntax highlighting
|
||||||
|
* Notepad++: `syntax-highlighting/notepad++.xml` ([how to install](https://stackoverflow.com/questions/4913834/how-do-you-add-syntax-highlighting-for-less-in-notepad))
|
@ -3,9 +3,7 @@ echo Building for Windows...
|
|||||||
cargo build --release
|
cargo build --release
|
||||||
echo Building for Linux...
|
echo Building for Linux...
|
||||||
cargo zigbuild --release --target x86_64-unknown-linux-gnu
|
cargo zigbuild --release --target x86_64-unknown-linux-gnu
|
||||||
echo Copy binaries to bin folder...
|
echo Copy to bin...
|
||||||
copy target\release\labast.exe bin\ /Y
|
copy target\release\labast.exe bin\ /Y
|
||||||
copy target\x86_64-unknown-linux-gnu\release\labast bin\ /Y
|
copy target\x86_64-unknown-linux-gnu\release\labast bin\ /Y
|
||||||
echo Creating source code tar.gz archive...
|
|
||||||
tar --exclude="./bin" --exclude="./target" --exclude=".git" -cvzf bin\labast-latest.tar.gz *
|
|
||||||
echo Done!
|
echo Done!
|
26
scripts/calc.lb
Normal file
26
scripts/calc.lb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
; written by Labashki developer - aeris
|
||||||
|
@ 0
|
||||||
|
scan
|
||||||
|
scan
|
||||||
|
scan
|
||||||
|
|jmp 0
|
||||||
|
|
||||||
|
@ 1
|
||||||
|
add
|
||||||
|
meow
|
||||||
|
quit
|
||||||
|
|
||||||
|
@ 2
|
||||||
|
sub
|
||||||
|
meow dkdkd
|
||||||
|
quit
|
||||||
|
|
||||||
|
@ 3
|
||||||
|
mul
|
||||||
|
meow
|
||||||
|
quit
|
||||||
|
|
||||||
|
@ 4
|
||||||
|
div
|
||||||
|
meow
|
||||||
|
quit
|
1
scripts/esolangs-program-forms/hello-world.lb
Normal file
1
scripts/esolangs-program-forms/hello-world.lb
Normal file
@ -0,0 +1 @@
|
|||||||
|
$println Hello, world!
|
@ -0,0 +1,4 @@
|
|||||||
|
; https://esolangs.org/wiki/User:XKCD_Random_Number
|
||||||
|
|
||||||
|
push 4
|
||||||
|
meow
|
12
scripts/esolangs-program-forms/truth-machine.lb
Normal file
12
scripts/esolangs-program-forms/truth-machine.lb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
; Truth-machine on Labashki
|
||||||
|
|
||||||
|
@ 0
|
||||||
|
scan
|
||||||
|
kjnz 1
|
||||||
|
meow
|
||||||
|
quit
|
||||||
|
|
||||||
|
@ 1
|
||||||
|
push 1
|
||||||
|
meow
|
||||||
|
jmp 1
|
9
scripts/expr.lb
Normal file
9
scripts/expr.lb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
; written by Labashki developer - aeris
|
||||||
|
@ 0
|
||||||
|
#expr >++:+\
|
||||||
|
dump
|
||||||
|
|
||||||
|
; Expected output
|
||||||
|
;
|
||||||
|
; 0: 3
|
||||||
|
; 1: 2
|
30
scripts/hello-world-old.lb
Normal file
30
scripts/hello-world-old.lb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
push 72 ; H
|
||||||
|
putc ; Print to console
|
||||||
|
push 101 ; e
|
||||||
|
putc
|
||||||
|
push 108 ; l
|
||||||
|
putc
|
||||||
|
push 108 ; l
|
||||||
|
putc
|
||||||
|
push 111 ; o
|
||||||
|
putc
|
||||||
|
push 32 ; <space>
|
||||||
|
putc
|
||||||
|
push 76 ; L
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
push 98 ; b
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
push 115 ; s
|
||||||
|
putc
|
||||||
|
push 104 ; h
|
||||||
|
putc
|
||||||
|
push 107 ; k
|
||||||
|
putc
|
||||||
|
push 105 ; i
|
||||||
|
putc
|
||||||
|
push 33 ; !
|
||||||
|
putc
|
1
scripts/hello-world.lb
Normal file
1
scripts/hello-world.lb
Normal file
@ -0,0 +1 @@
|
|||||||
|
$print Hello Labashki!
|
20
scripts/labels.lb
Normal file
20
scripts/labels.lb
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
@ 1
|
||||||
|
push 115 ; s
|
||||||
|
putc
|
||||||
|
push 104 ; h
|
||||||
|
putc
|
||||||
|
push 107 ; k
|
||||||
|
putc
|
||||||
|
push 105 ; i
|
||||||
|
putc
|
||||||
|
exit
|
||||||
|
@ 0
|
||||||
|
push 76 ; L
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
push 98 ; b
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
jmp 1
|
23
scripts/math.lb
Normal file
23
scripts/math.lb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
; Addition
|
||||||
|
push 2 ; a = 2
|
||||||
|
push 3 ; b = 2
|
||||||
|
add ; a + b
|
||||||
|
meow ; = 5
|
||||||
|
|
||||||
|
; Subtraction
|
||||||
|
push 9 ; a = 9
|
||||||
|
push 4 ; b = 4
|
||||||
|
sub ; b - a
|
||||||
|
meow ; = 5
|
||||||
|
|
||||||
|
; Multiplication
|
||||||
|
push 9 ; a = 9
|
||||||
|
push 5 ; b = 5
|
||||||
|
mul ; b * a
|
||||||
|
meow ; = 45
|
||||||
|
|
||||||
|
; Division
|
||||||
|
push 81; a = 81
|
||||||
|
push 9 ; b = 9
|
||||||
|
div ; b / a
|
||||||
|
meow ; = 9
|
12
scripts/modules/main.lb
Normal file
12
scripts/modules/main.lb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
push 76 ; L
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
push 98 ; b
|
||||||
|
putc
|
||||||
|
push 97 ; a
|
||||||
|
putc
|
||||||
|
#exec second_part.lb
|
||||||
|
push 33 ; !
|
||||||
|
putc
|
||||||
|
quit
|
8
scripts/modules/second_part.lb
Normal file
8
scripts/modules/second_part.lb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
push 115 ; s
|
||||||
|
putc
|
||||||
|
push 104 ; h
|
||||||
|
putc
|
||||||
|
push 107 ; k
|
||||||
|
putc
|
||||||
|
push 105 ; i
|
||||||
|
putc
|
15
scripts/pick.lb
Normal file
15
scripts/pick.lb
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
; written by Labashki developer - aeris
|
||||||
|
@ 0
|
||||||
|
push 2
|
||||||
|
push 3
|
||||||
|
push 4
|
||||||
|
push 1
|
||||||
|
pick
|
||||||
|
dump
|
||||||
|
|
||||||
|
; Expected output
|
||||||
|
;
|
||||||
|
; 0: 2
|
||||||
|
; 1: 3
|
||||||
|
; 2: 4
|
||||||
|
; 3: 3
|
10
scripts/size.lb
Normal file
10
scripts/size.lb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
@ 0
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
push 3
|
||||||
|
size
|
||||||
|
meow
|
||||||
|
|
||||||
|
; Expected output
|
||||||
|
;
|
||||||
|
; 3
|
16
scripts/std/print.lb
Normal file
16
scripts/std/print.lb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
;
|
||||||
|
; BSD Zero Clause License
|
||||||
|
;
|
||||||
|
; Copyright (C) 2024 by Bedohswe
|
||||||
|
;
|
||||||
|
; Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
|
||||||
|
;
|
||||||
|
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
@ 0
|
||||||
|
args 1
|
||||||
|
kjz 1
|
||||||
|
putc
|
||||||
|
jmp 0
|
||||||
|
@ 1
|
||||||
|
quit
|
4
scripts/std/rev.lb
Normal file
4
scripts/std/rev.lb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
; Labashki standard library
|
||||||
|
; Written by Labashki developer - aeris
|
||||||
|
|
||||||
|
args 0
|
4
scripts/std/rot.lb
Normal file
4
scripts/std/rot.lb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
; Labashki standard library
|
||||||
|
; Written by Labashki developer - aeris
|
||||||
|
|
||||||
|
args 3
|
17
scripts/std/unix/ВАРЁНЫЙНОСОК.lb
Normal file
17
scripts/std/unix/ВАРЁНЫЙНОСОК.lb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
; Labashki standard library
|
||||||
|
; Written by Labashki developer - aeris
|
||||||
|
|
||||||
|
maxsize
|
||||||
|
args 1 ; Насколько сварить носок
|
||||||
|
sub
|
||||||
|
|
||||||
|
@ 1
|
||||||
|
jnz 2
|
||||||
|
quit
|
||||||
|
|
||||||
|
@ 2
|
||||||
|
#expr -
|
||||||
|
push -1
|
||||||
|
_unix_random
|
||||||
|
swap
|
||||||
|
jmp 1
|
@ -1,19 +1,5 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
pub enum StackError {
|
|
||||||
StackOverflow,
|
|
||||||
StackUnderflow,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for StackError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
StackError::StackOverflow => write!(f, "Stack overflow!"),
|
|
||||||
StackError::StackUnderflow => write!(f, "Stack underflow!"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum RunError {
|
pub enum RunError {
|
||||||
RequestArgsInMainModule,
|
RequestArgsInMainModule,
|
||||||
ExecuteItself(String),
|
ExecuteItself(String),
|
||||||
@ -25,36 +11,27 @@ pub enum RunError {
|
|||||||
FailToReadLineFromConsole,
|
FailToReadLineFromConsole,
|
||||||
UnknownLabel(u16),
|
UnknownLabel(u16),
|
||||||
InvalidExpressionUnknownOperator(String),
|
InvalidExpressionUnknownOperator(String),
|
||||||
|
MemoryEmpty,
|
||||||
AttemptToInsertHashOrStringInst,
|
AttemptToInsertHashOrStringInst,
|
||||||
AttemptToInsertEmptyConst,
|
AttemptToInsertEmptyConst,
|
||||||
AttemptToInsertALabel,
|
AttemptToInsertALabel
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for RunError {
|
impl Display for RunError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
RunError::RequestArgsInMainModule => {
|
RunError::RequestArgsInMainModule => write!(f, "Can't require arguments in the main module"),
|
||||||
write!(f, "Can't require arguments in the main module")
|
RunError::ExecuteItself(mod_name) => write!(f, "Module {}: Can't execute itself", mod_name),
|
||||||
}
|
|
||||||
RunError::ExecuteItself(mod_name) => {
|
|
||||||
write!(f, "Module {}: Can't execute itself", mod_name)
|
|
||||||
}
|
|
||||||
RunError::FailToReadFile => write!(f, "Unable to read file"),
|
RunError::FailToReadFile => write!(f, "Unable to read file"),
|
||||||
RunError::FailToGetCharFromConsole => write!(f, "Failed to get character from console"),
|
RunError::FailToGetCharFromConsole => write!(f, "Failed to get character from console"),
|
||||||
RunError::InvalidInputUShortInt => write!(
|
RunError::InvalidInputUShortInt => write!(f, "Invalid input. Please enter a valid unsigned short integer"),
|
||||||
f,
|
|
||||||
"Invalid input. Please enter a valid unsigned short integer"
|
|
||||||
),
|
|
||||||
RunError::PickTooDeep => write!(f, "Picking too deep"),
|
RunError::PickTooDeep => write!(f, "Picking too deep"),
|
||||||
RunError::PickOutOfBounds => write!(f, "Trying to get a value out of bounds"),
|
RunError::PickOutOfBounds => write!(f, "Trying to get a value out of bounds"),
|
||||||
RunError::FailToReadLineFromConsole => write!(f, "Failed to read line from console"),
|
RunError::FailToReadLineFromConsole => write!(f, "Failed to read line from console"),
|
||||||
RunError::UnknownLabel(label_name) => write!(f, "Unknown label: {}", label_name),
|
RunError::UnknownLabel(label_name) => write!(f, "Unknown label: {}", label_name),
|
||||||
RunError::InvalidExpressionUnknownOperator(name) => {
|
RunError::InvalidExpressionUnknownOperator(name) => write!(f, "Invalid expression: unknown operator '{}'", name),
|
||||||
write!(f, "Invalid expression: unknown operator '{}'", name)
|
RunError::MemoryEmpty => write!(f, "No elements in memory!"),
|
||||||
}
|
RunError::AttemptToInsertHashOrStringInst => write!(f, "Attempt to insert a hash/string-instruction"),
|
||||||
RunError::AttemptToInsertHashOrStringInst => {
|
|
||||||
write!(f, "Attempt to insert a hash/string-instruction")
|
|
||||||
}
|
|
||||||
RunError::AttemptToInsertEmptyConst => write!(f, "Attempt to insert an empty constant"),
|
RunError::AttemptToInsertEmptyConst => write!(f, "Attempt to insert an empty constant"),
|
||||||
RunError::AttemptToInsertALabel => write!(f, "Attempt to insert a label"),
|
RunError::AttemptToInsertALabel => write!(f, "Attempt to insert a label"),
|
||||||
}
|
}
|
||||||
@ -62,17 +39,13 @@ impl Display for RunError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum ParseError {
|
pub enum ParseError {
|
||||||
ArgumentNotRequired(String, usize, String),
|
ArgumentNotRequired(String, usize, String)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ParseError {
|
impl Display for ParseError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ParseError::ArgumentNotRequired(name, line_number, line) => write!(
|
ParseError::ArgumentNotRequired(name, line_number, line) => write!(f, "ParseError:\n{1} | {2}\nerror: string argument for {0} not required", name, line_number, line),
|
||||||
f,
|
|
||||||
"ParseError:\n{1} | {2}\nerror: string argument for {0} not required",
|
|
||||||
name, line_number, line
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
193
src/execute.rs
193
src/execute.rs
@ -1,7 +1,4 @@
|
|||||||
use crate::{
|
use crate::{errors::RunError, instructions, stack::{Instruction, Stack}};
|
||||||
instructions,
|
|
||||||
stack::{Instruction, Stack},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn find_labels(stack: &mut Stack) {
|
fn find_labels(stack: &mut Stack) {
|
||||||
while stack.program_counter < (stack.program.len() as u16) {
|
while stack.program_counter < (stack.program.len() as u16) {
|
||||||
@ -14,7 +11,7 @@ fn find_labels(stack: &mut Stack) {
|
|||||||
|
|
||||||
if stack.labels[label.data as usize].is_some() {
|
if stack.labels[label.data as usize].is_some() {
|
||||||
eprintln!("Label {} already defined", label.data);
|
eprintln!("Label {} already defined", label.data);
|
||||||
std::process::exit(2);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if label.data == 0 {
|
if label.data == 0 {
|
||||||
@ -25,47 +22,42 @@ fn find_labels(stack: &mut Stack) {
|
|||||||
stack.labels[label.data as usize] = Some(stack.program_counter as i16);
|
stack.labels[label.data as usize] = Some(stack.program_counter as i16);
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.program_counter = stack.labels[0].unwrap_or(0) as u16;
|
if stack.labels[0].is_some() {
|
||||||
|
stack.program_counter = stack.labels[0].unwrap() as u16;
|
||||||
|
} else {
|
||||||
|
stack.program_counter = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_prefix_operators(instruction: &mut Instruction) {
|
fn find_prefix_operators(instruction: &mut Instruction, memory: &mut Vec<u16>) {
|
||||||
match instruction.name.chars().nth(0) {
|
match instruction.name.chars().nth(0) {
|
||||||
Some('|') => {
|
Some('|') => {
|
||||||
instruction.name = instruction.name.chars().skip(1).collect();
|
instruction.name = instruction.name.chars().skip(1).collect();
|
||||||
|
|
||||||
instruction.ispiped = true;
|
instruction.data = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
}
|
},
|
||||||
Some('/') => {
|
Some('^') => {
|
||||||
instruction.name = instruction.name.chars().skip(1).collect();
|
instruction.name = instruction.name.chars().skip(1).collect();
|
||||||
|
//dbg!(&instruction);
|
||||||
instruction.isdrained = true;
|
if instruction.name.chars().nth(0) != Some('#') {
|
||||||
|
println!("{0} {1}", instruction.name, instruction.data.to_string());
|
||||||
|
} else if instruction.arg.is_empty() {
|
||||||
|
println!("{}", instruction.name);
|
||||||
|
} else {
|
||||||
|
println!("{0} {1}", instruction.name, instruction.arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name: String) {
|
pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name: &String) {
|
||||||
find_labels(stack);
|
find_labels(stack);
|
||||||
|
|
||||||
while let Some(mut instruction) = stack.program.get_mut(stack.program_counter as usize) {
|
while let Some(mut instruction) = stack.program.get_mut(stack.program_counter as usize) {
|
||||||
stack.program_counter += 1;
|
stack.program_counter += 1;
|
||||||
|
|
||||||
find_prefix_operators(&mut instruction);
|
find_prefix_operators(&mut instruction, &mut stack.memory);
|
||||||
|
|
||||||
if instruction.ispiped {
|
|
||||||
instruction.data = stack.memory.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
if instruction.isdrained {
|
|
||||||
stack.memory.push(instruction.data);
|
|
||||||
instruction.data = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if instruction.name.chars().nth(0) == Some('#')
|
|
||||||
|| instruction.name.chars().nth(0) == Some('$')
|
|
||||||
{
|
|
||||||
instruction.name = instruction.name.chars().skip(1).collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
if instruction.name.starts_with('@') {
|
if instruction.name.starts_with('@') {
|
||||||
continue;
|
continue;
|
||||||
@ -73,120 +65,81 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
|
|||||||
|
|
||||||
match instruction.name.as_str() {
|
match instruction.name.as_str() {
|
||||||
// Stack operations
|
// Stack operations
|
||||||
"push" => instructions::stack_manage::push(&mut stack.memory, instruction.data),
|
"push" => instructions::push::push(&mut stack.memory, &instruction.data),
|
||||||
"pop" => instructions::stack_manage::pop(&mut stack.memory),
|
"pop" => instructions::pop::pop(&mut stack.memory),
|
||||||
"dup" => instructions::stack_manage::dup(&mut stack.memory),
|
"dup" => instructions::dup::dup(&mut stack.memory),
|
||||||
"swap" => instructions::stack_manage::swap(&mut stack.memory),
|
"swap" => instructions::swap::swap(&mut stack.memory),
|
||||||
"pick" => instructions::stack_manage::pick(&mut stack.memory, instruction.data),
|
"pick" => instructions::pick::pick(&mut stack.memory, &instruction.data),
|
||||||
"string" => instructions::stack_manage::string(&mut stack.memory, instruction.arg.clone()),
|
|
||||||
|
|
||||||
// Math operations
|
// Math
|
||||||
"add" => instructions::math::add(&mut stack.memory),
|
"add" => instructions::add::add(&mut stack.memory),
|
||||||
"sub" => instructions::math::sub(&mut stack.memory),
|
"sub" => instructions::sub::sub(&mut stack.memory),
|
||||||
"mul" => instructions::math::mul(&mut stack.memory),
|
"mul" => instructions::mul::mul(&mut stack.memory),
|
||||||
"div" => instructions::math::div(&mut stack.memory),
|
"div" => instructions::div::div(&mut stack.memory),
|
||||||
"mod" => instructions::math::modulo(&mut stack.memory),
|
"mod" => instructions::modulo::modulo(&mut stack.memory),
|
||||||
|
|
||||||
// Logic
|
// Logic
|
||||||
"and" => instructions::logic::and(&mut stack.memory),
|
"and" => instructions::and::and(&mut stack.memory),
|
||||||
"or" => instructions::logic::or(&mut stack.memory),
|
"or" => instructions::or::or(&mut stack.memory),
|
||||||
"xor" => instructions::logic::xor(&mut stack.memory),
|
"xor" => instructions::xor::xor(&mut stack.memory),
|
||||||
"nand" => instructions::logic::nand(&mut stack.memory),
|
"nand" => instructions::nand::nand(&mut stack.memory),
|
||||||
"not" => instructions::logic::not(&mut stack.memory),
|
"not" => instructions::not::not(&mut stack.memory),
|
||||||
|
|
||||||
// Bitwise
|
// Bitwise
|
||||||
"band" => instructions::bitwise::band(&mut stack.memory),
|
"band" => instructions::band::band(&mut stack.memory),
|
||||||
"bor" => instructions::bitwise::bor(&mut stack.memory),
|
"bor" => instructions::bor::bor(&mut stack.memory),
|
||||||
"bxor" => instructions::bitwise::bxor(&mut stack.memory),
|
"bxor" => instructions::bxor::bxor(&mut stack.memory),
|
||||||
"bnand" => instructions::bitwise::bnand(&mut stack.memory),
|
"bnand" => instructions::bnand::bnand(&mut stack.memory),
|
||||||
"bnot" => instructions::logic::not(&mut stack.memory),
|
"bnot" => instructions::not::not(&mut stack.memory),
|
||||||
|
|
||||||
// Flow-control
|
// Labels
|
||||||
"jmp" => instructions::flow_control::jmp(
|
"jmp" => instructions::jmp::jmp(&mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||||
&mut stack.labels,
|
"jnz" => instructions::jnz::jnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||||
&mut stack.program_counter,
|
"jz" => instructions::jz::jz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||||
instruction.data,
|
"kjnz" => instructions::kjnz::kjnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||||
),
|
"kjz" => instructions::kjz::kjz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||||
"rjmp" => instructions::specific::rjmp(
|
|
||||||
&mut stack.memory,
|
|
||||||
&mut stack.labels,
|
|
||||||
&mut stack.program_counter,
|
|
||||||
),
|
|
||||||
"jnz" => instructions::flow_control::jnz(
|
|
||||||
&mut stack.memory,
|
|
||||||
&mut stack.labels,
|
|
||||||
&mut stack.program_counter,
|
|
||||||
instruction.data,
|
|
||||||
),
|
|
||||||
"jz" => instructions::flow_control::jz(
|
|
||||||
&mut stack.memory,
|
|
||||||
&mut stack.labels,
|
|
||||||
&mut stack.program_counter,
|
|
||||||
instruction.data,
|
|
||||||
),
|
|
||||||
"kjnz" => instructions::flow_control::kjnz(
|
|
||||||
&mut stack.memory,
|
|
||||||
&mut stack.labels,
|
|
||||||
&mut stack.program_counter,
|
|
||||||
instruction.data,
|
|
||||||
),
|
|
||||||
"kjz" => instructions::flow_control::kjz(
|
|
||||||
&mut stack.memory,
|
|
||||||
&mut stack.labels,
|
|
||||||
&mut stack.program_counter,
|
|
||||||
instruction.data,
|
|
||||||
),
|
|
||||||
|
|
||||||
// Input/Output
|
// Input/Output
|
||||||
"putc" => instructions::io::putc(&mut stack.memory),
|
"putc" => instructions::putc::putc(&mut stack.memory),
|
||||||
"getc" => instructions::io::getc(&mut stack.memory),
|
"getc" => instructions::getc::getc(&mut stack.memory),
|
||||||
"scan" => instructions::io::scan(&mut stack.memory),
|
"scan" => instructions::scan::scan(&mut stack.memory),
|
||||||
"meow" => instructions::io::meow(&mut stack.memory),
|
"meow" => instructions::meow::meow(&mut stack.memory),
|
||||||
"print" => instructions::io::print(instruction.arg.clone()),
|
"#print" => instructions::print::print(&instruction.arg),
|
||||||
"println" => instructions::io::println(instruction.arg.clone()),
|
"$print" => instructions::print::print(&instruction.arg),
|
||||||
|
"#println" => instructions::println::println(&instruction.arg),
|
||||||
|
"$println" => instructions::println::println(&instruction.arg),
|
||||||
|
|
||||||
// Modules
|
// Modules
|
||||||
"args" => {
|
"args" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data),
|
||||||
instructions::modules::args(&mut stack.memory, &mut origin_stack, instruction.data)
|
"#exec" => {
|
||||||
|
let arg = instruction.arg.clone();
|
||||||
|
instructions::exec::exec(stack, &arg, &mod_name);
|
||||||
}
|
}
|
||||||
"exec" => {
|
|
||||||
let arg = instruction.arg.clone();
|
|
||||||
instructions::modules::exec(stack, arg, mod_name.clone())
|
|
||||||
},
|
|
||||||
"call" => {
|
|
||||||
let arg = instruction.arg.clone();
|
|
||||||
instructions::modules::call(stack, arg)
|
|
||||||
},
|
|
||||||
"native" => {
|
|
||||||
let arg = instruction.arg.clone();
|
|
||||||
unsafe {instructions::native::native(stack, arg)}
|
|
||||||
},
|
|
||||||
|
|
||||||
// Self-modifying
|
// Self-modifying
|
||||||
"insert" => {
|
|
||||||
|
"#insert" => {
|
||||||
let arg = instruction.arg.clone();
|
let arg = instruction.arg.clone();
|
||||||
let data = instruction.data.clone();
|
let data = instruction.data.clone();
|
||||||
instructions::self_modify::insert(&mut stack.program, arg, data)
|
|
||||||
}
|
instructions::insert::insert(&mut stack.program, &arg, &data)
|
||||||
|
},
|
||||||
|
|
||||||
// Special
|
// Special
|
||||||
"dump" => instructions::special::dump(&mut stack.memory),
|
"dump" => instructions::dump::dump(&mut stack.memory),
|
||||||
"size" => instructions::special::size(&mut stack.memory),
|
"size" => instructions::size::size(&mut stack.memory),
|
||||||
"maxsize" => instructions::special::maxsize(&mut stack.memory),
|
"maxsize" => instructions::maxsize::maxsize(&mut stack.memory),
|
||||||
"expr" => instructions::special::expr(&mut stack.memory, instruction.arg.clone()),
|
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
|
||||||
"field" => continue,
|
"nop" => continue,
|
||||||
"quit" => break,
|
"quit" => break,
|
||||||
"exit" => instructions::special::exit(instruction.data),
|
"exit" => instructions::exit::exit(&instruction.data),
|
||||||
|
|
||||||
// Platform-specific
|
// Platform-specific
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
"_unix_random" => instructions::platform_specific::_unix_random(&mut stack.memory),
|
"_unix_random" => instructions::_unix_random::_unix_random(&mut stack.memory),
|
||||||
|
|
||||||
#[cfg(not(target_family = "unix"))]
|
|
||||||
"_unix_random" => instructions::specific::random(&mut stack.memory),
|
|
||||||
|
|
||||||
// Labast-specific
|
// Labast-specific
|
||||||
"random" => instructions::specific::random(&mut stack.memory),
|
"random" => instructions::random::random(&mut stack.memory),
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Unknown instruction: {}", instruction.name);
|
eprintln!("Unknown instruction: {}", instruction.name);
|
||||||
|
14
src/instructions/_unix_random.rs
Normal file
14
src/instructions/_unix_random.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use crate::RunError;
|
||||||
|
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
pub fn _unix_random(memory: &mut Vec<u16>) {
|
||||||
|
let mut rng = File::open("/dev/urandom").unwrap();
|
||||||
|
|
||||||
|
let mut buffer = [0u8; 2];
|
||||||
|
rng.read_exact(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
memory.push((u16::from_ne_bytes(buffer)) % (*data - 1));
|
||||||
|
}
|
8
src/instructions/add.rs
Normal file
8
src/instructions/add.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn add(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(a + b);
|
||||||
|
}
|
14
src/instructions/and.rs
Normal file
14
src/instructions/and.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn and(memory: &mut Vec<u16>) {
|
||||||
|
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
|
||||||
|
let c: u16 = if a && b {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
memory.push(c);
|
||||||
|
}
|
18
src/instructions/args.rs
Normal file
18
src/instructions/args.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use crate::{errors::RunError, stack::Stack};
|
||||||
|
|
||||||
|
pub fn args(memory: &mut Vec<u16>, origin_stack: &mut Option<&mut Stack>, data: &u16) {
|
||||||
|
if let Some(origin_stack) = origin_stack.as_mut() {
|
||||||
|
let mut data = *data;
|
||||||
|
|
||||||
|
if data == 0 {
|
||||||
|
data = origin_stack.memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..data {
|
||||||
|
memory.push(origin_stack.memory.pop().expect(&format!("{}", RunError::MemoryEmpty)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eprintln!("{}", RunError::RequestArgsInMainModule);
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
}
|
8
src/instructions/band.rs
Normal file
8
src/instructions/band.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn band(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(a & b);
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
use crate::stack::StackVec;
|
|
||||||
|
|
||||||
pub fn band(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(a & b);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bor(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(a | b);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bxor(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(a ^ b);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bnand(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(!(a & b));
|
|
||||||
}
|
|
8
src/instructions/bnand.rs
Normal file
8
src/instructions/bnand.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn bnand(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(!(a & b));
|
||||||
|
}
|
8
src/instructions/bor.rs
Normal file
8
src/instructions/bor.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn bor(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(a | b);
|
||||||
|
}
|
8
src/instructions/bxor.rs
Normal file
8
src/instructions/bxor.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn bxor(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(a ^ b);
|
||||||
|
}
|
8
src/instructions/div.rs
Normal file
8
src/instructions/div.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn div(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(b / a);
|
||||||
|
}
|
10
src/instructions/dump.rs
Normal file
10
src/instructions/dump.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
pub fn dump(memory: &mut Vec<u16>) {
|
||||||
|
if memory.is_empty() {
|
||||||
|
println!("Stack is empty.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i, entry) in memory.iter().enumerate() {
|
||||||
|
println!("{0}: {1}", i, entry);
|
||||||
|
}
|
||||||
|
}
|
5
src/instructions/dup.rs
Normal file
5
src/instructions/dup.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn dup(memory: &mut Vec<u16>) {
|
||||||
|
memory.push(*memory.last().expect(&format!("{}", RunError::MemoryEmpty)));
|
||||||
|
}
|
19
src/instructions/exec.rs
Normal file
19
src/instructions/exec.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use std::fs;
|
||||||
|
use crate::{parse, execute, Stack, errors::RunError};
|
||||||
|
|
||||||
|
pub fn exec(mut stack: &mut Stack, arg: &String, mod_name: &String) {
|
||||||
|
if mod_name == arg {
|
||||||
|
eprintln!("{}", RunError::ExecuteItself(arg.to_string()));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
let lines = fs::read_to_string(&arg).expect(&format!("{}", RunError::FailToReadFile));
|
||||||
|
let mut temp_stack = Stack::new();
|
||||||
|
|
||||||
|
parse(&mut temp_stack, &lines);
|
||||||
|
execute(&mut temp_stack, Some(&mut stack), &arg.clone());
|
||||||
|
|
||||||
|
for item in temp_stack.memory {
|
||||||
|
stack.memory.push(item);
|
||||||
|
}
|
||||||
|
}
|
3
src/instructions/exit.rs
Normal file
3
src/instructions/exit.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn exit(data: &u16) {
|
||||||
|
std::process::exit((*data).into());
|
||||||
|
}
|
31
src/instructions/expr.rs
Normal file
31
src/instructions/expr.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn expr(memory: &mut Vec<u16>, arg: &String) {
|
||||||
|
for code in arg.chars() {
|
||||||
|
match code {
|
||||||
|
'>' => memory.push(0),
|
||||||
|
'$' => { memory.pop(); },
|
||||||
|
':' => memory.push(*memory.last().expect(&format!("{}", RunError::MemoryEmpty))),
|
||||||
|
'\\' => {
|
||||||
|
let ax = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let ax2 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(ax);
|
||||||
|
memory.push(ax2);
|
||||||
|
}
|
||||||
|
'+' => {
|
||||||
|
let ax = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
memory.push(ax + 1);
|
||||||
|
}
|
||||||
|
'-' => {
|
||||||
|
let ax = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
memory.push(ax - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
eprintln!("{}", RunError::InvalidExpressionUnknownOperator(code.to_string()));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,74 +0,0 @@
|
|||||||
use crate::{errors::RunError, stack::StackVec};
|
|
||||||
|
|
||||||
pub fn jmp(labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: u16) {
|
|
||||||
if labels[data as usize].is_none() {
|
|
||||||
eprintln!("{}", RunError::UnknownLabel(data));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
*program_counter = (labels[data as usize].unwrap() - 1) as u16;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn jnz(
|
|
||||||
memory: &mut StackVec,
|
|
||||||
labels: &mut [Option<i16>; 256],
|
|
||||||
program_counter: &mut u16,
|
|
||||||
data: u16,
|
|
||||||
) {
|
|
||||||
if labels[data as usize].is_none() {
|
|
||||||
eprintln!("{}", RunError::UnknownLabel(data));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if memory.pop() != 0 {
|
|
||||||
*program_counter = (labels[data as usize].unwrap() - 1) as u16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn jz(
|
|
||||||
memory: &mut StackVec,
|
|
||||||
labels: &mut [Option<i16>; 256],
|
|
||||||
program_counter: &mut u16,
|
|
||||||
data: u16,
|
|
||||||
) {
|
|
||||||
if labels[data as usize].is_none() {
|
|
||||||
eprintln!("{}", RunError::UnknownLabel(data));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if memory.pop() == 0 {
|
|
||||||
*program_counter = (labels[data as usize].unwrap() - 1) as u16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn kjnz(
|
|
||||||
memory: &mut StackVec,
|
|
||||||
labels: &mut [Option<i16>; 256],
|
|
||||||
program_counter: &mut u16,
|
|
||||||
data: u16,
|
|
||||||
) {
|
|
||||||
if labels[data as usize].is_none() {
|
|
||||||
eprintln!("{}", RunError::UnknownLabel(data));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if memory.last() != 0 {
|
|
||||||
*program_counter = (labels[data as usize].unwrap() - 1) as u16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn kjz(
|
|
||||||
memory: &mut StackVec,
|
|
||||||
labels: &mut [Option<i16>; 256],
|
|
||||||
program_counter: &mut u16,
|
|
||||||
data: u16,
|
|
||||||
) {
|
|
||||||
if labels[data as usize].is_none() {
|
|
||||||
eprintln!("{}", RunError::UnknownLabel(data));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if memory.last() == 0 {
|
|
||||||
*program_counter = (labels[data as usize].unwrap() - 1) as u16;
|
|
||||||
}
|
|
||||||
}
|
|
13
src/instructions/getc.rs
Normal file
13
src/instructions/getc.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn getc(memory: &mut Vec<u16>) {
|
||||||
|
let char = std::io::stdin()
|
||||||
|
.bytes()
|
||||||
|
.next()
|
||||||
|
.and_then(|result| result.ok())
|
||||||
|
.map(|byte| byte as u16)
|
||||||
|
.expect(&format!("{}", RunError::FailToGetCharFromConsole));
|
||||||
|
|
||||||
|
memory.push(char);
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{errors::RunError, stack::Instruction};
|
use crate::{errors::RunError, stack::Instruction};
|
||||||
|
|
||||||
pub fn insert(program: &mut Vec<Instruction>, arg: String, data: u16) {
|
pub fn insert(program: &mut Vec<Instruction>, arg: &String, data: &u16) {
|
||||||
let args: Vec<char> = arg.chars().collect();
|
let args: Vec<char> = arg.chars().collect();
|
||||||
|
|
||||||
if args[0] == '#' || args[0] == '$' {
|
if args[0] == '#' || args[0] == '$' {
|
||||||
@ -21,8 +21,6 @@ pub fn insert(program: &mut Vec<Instruction>, arg: String, data: u16) {
|
|||||||
program.push(Instruction {
|
program.push(Instruction {
|
||||||
name: arg.to_string(),
|
name: arg.to_string(),
|
||||||
arg: "\n".to_string(),
|
arg: "\n".to_string(),
|
||||||
data,
|
data: *data
|
||||||
ispiped: false,
|
|
||||||
isdrained: false
|
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -1,43 +0,0 @@
|
|||||||
use crate::{errors::RunError, stack::StackVec};
|
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
pub fn putc(memory: &mut StackVec) {
|
|
||||||
print!("{}", memory.pop() as u8 as char);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getc(memory: &mut StackVec) {
|
|
||||||
let char = std::io::stdin()
|
|
||||||
.bytes()
|
|
||||||
.next()
|
|
||||||
.and_then(|result| result.ok())
|
|
||||||
.map(|byte| byte as u16)
|
|
||||||
.expect(&format!("{}", RunError::FailToGetCharFromConsole));
|
|
||||||
|
|
||||||
memory.push(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scan(memory: &mut StackVec) {
|
|
||||||
let mut input = String::new();
|
|
||||||
|
|
||||||
std::io::stdin()
|
|
||||||
.read_line(&mut input)
|
|
||||||
.expect(&format!("{}", RunError::FailToReadLineFromConsole));
|
|
||||||
|
|
||||||
let num = input
|
|
||||||
.trim()
|
|
||||||
.parse()
|
|
||||||
.expect(&format!("{}", RunError::InvalidInputUShortInt));
|
|
||||||
memory.push(num);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn meow(memory: &mut StackVec) {
|
|
||||||
println!("{}", memory.pop());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn print(arg: String) {
|
|
||||||
print!("{}", arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn println(arg: String) {
|
|
||||||
println!("{}", arg);
|
|
||||||
}
|
|
10
src/instructions/jmp.rs
Normal file
10
src/instructions/jmp.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn jmp(labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
|
||||||
|
if labels[*data as usize].is_none() {
|
||||||
|
eprintln!("{}", RunError::UnknownLabel(*data));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
|
||||||
|
}
|
12
src/instructions/jnz.rs
Normal file
12
src/instructions/jnz.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn jnz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
|
||||||
|
if labels[*data as usize].is_none() {
|
||||||
|
eprintln!("{}", RunError::UnknownLabel(*data));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if memory.pop() != Some(0) {
|
||||||
|
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
|
||||||
|
}
|
||||||
|
}
|
12
src/instructions/jz.rs
Normal file
12
src/instructions/jz.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn jz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
|
||||||
|
if labels[*data as usize].is_none() {
|
||||||
|
eprintln!("{}", RunError::UnknownLabel(*data));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if memory.pop() == Some(0) {
|
||||||
|
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
|
||||||
|
}
|
||||||
|
}
|
12
src/instructions/kjnz.rs
Normal file
12
src/instructions/kjnz.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn kjnz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
|
||||||
|
if labels[*data as usize].is_none() {
|
||||||
|
eprintln!("{}", RunError::UnknownLabel(*data));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if memory.last() != Some(&0) {
|
||||||
|
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
|
||||||
|
}
|
||||||
|
}
|
12
src/instructions/kjz.rs
Normal file
12
src/instructions/kjz.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn kjz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
|
||||||
|
if labels[*data as usize].is_none() {
|
||||||
|
eprintln!("{}", RunError::UnknownLabel(*data));
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if memory.last() == Some(&0) {
|
||||||
|
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
|
||||||
|
}
|
||||||
|
}
|
@ -1,45 +0,0 @@
|
|||||||
use crate::stack::StackVec;
|
|
||||||
|
|
||||||
pub fn and(memory: &mut StackVec) {
|
|
||||||
let a: bool = memory.pop() == 1;
|
|
||||||
let b: bool = memory.pop() == 1;
|
|
||||||
|
|
||||||
let c: u16 = if a && b { 1 } else { 0 };
|
|
||||||
|
|
||||||
memory.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn or(memory: &mut StackVec) {
|
|
||||||
let a: bool = memory.pop() == 1;
|
|
||||||
let b: bool = memory.pop() == 1;
|
|
||||||
|
|
||||||
let c: u16 = if a || b { 1 } else { 0 };
|
|
||||||
|
|
||||||
memory.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn xor(memory: &mut StackVec) {
|
|
||||||
let a: bool = memory.pop() == 1;
|
|
||||||
let b: bool = memory.pop() == 1;
|
|
||||||
|
|
||||||
let c: u16 = if !a != !b { 1 } else { 0 };
|
|
||||||
|
|
||||||
memory.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn nand(memory: &mut StackVec) {
|
|
||||||
let a: bool = memory.pop() == 1;
|
|
||||||
let b: bool = memory.pop() == 1;
|
|
||||||
|
|
||||||
let c: u16 = if !(a && b) { 1 } else { 0 };
|
|
||||||
|
|
||||||
memory.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn not(memory: &mut StackVec) {
|
|
||||||
let a: bool = memory.pop() == 1;
|
|
||||||
|
|
||||||
let c: u16 = if !a { 1 } else { 0 };
|
|
||||||
|
|
||||||
memory.push(c);
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
use crate::stack::StackVec;
|
|
||||||
|
|
||||||
pub fn add(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(a + b);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sub(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(b - a);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mul(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(b * a);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn div(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(b / a);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn modulo(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(b % a);
|
|
||||||
}
|
|
3
src/instructions/maxsize.rs
Normal file
3
src/instructions/maxsize.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn maxsize(memory: &mut Vec<u16>) {
|
||||||
|
memory.push(u16::MAX);
|
||||||
|
}
|
5
src/instructions/meow.rs
Normal file
5
src/instructions/meow.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn meow(memory: &mut Vec<u16>) {
|
||||||
|
println!("{}", memory.pop().expect(&format!("{}", RunError::MemoryEmpty)));
|
||||||
|
}
|
@ -1,12 +1,41 @@
|
|||||||
pub mod bitwise;
|
pub mod push;
|
||||||
pub mod flow_control;
|
pub mod pop;
|
||||||
pub mod io;
|
pub mod dup;
|
||||||
pub mod logic;
|
pub mod swap;
|
||||||
pub mod math;
|
pub mod add;
|
||||||
pub mod modules;
|
pub mod sub;
|
||||||
pub mod platform_specific;
|
pub mod mul;
|
||||||
pub mod self_modify;
|
pub mod div;
|
||||||
pub mod special;
|
pub mod jmp;
|
||||||
pub mod specific;
|
pub mod jnz;
|
||||||
pub mod stack_manage;
|
pub mod jz;
|
||||||
pub mod native;
|
pub mod pick;
|
||||||
|
pub mod putc;
|
||||||
|
pub mod getc;
|
||||||
|
pub mod meow;
|
||||||
|
pub mod dump;
|
||||||
|
pub mod scan;
|
||||||
|
pub mod exec;
|
||||||
|
pub mod args;
|
||||||
|
pub mod expr;
|
||||||
|
pub mod size;
|
||||||
|
pub mod exit;
|
||||||
|
pub mod maxsize;
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
pub mod _unix_random;
|
||||||
|
pub mod modulo;
|
||||||
|
pub mod and;
|
||||||
|
pub mod or;
|
||||||
|
pub mod xor;
|
||||||
|
pub mod nand;
|
||||||
|
pub mod band;
|
||||||
|
pub mod bor;
|
||||||
|
pub mod bxor;
|
||||||
|
pub mod bnand;
|
||||||
|
pub mod not;
|
||||||
|
pub mod kjnz;
|
||||||
|
pub mod kjz;
|
||||||
|
pub mod print;
|
||||||
|
pub mod println;
|
||||||
|
pub mod insert;
|
||||||
|
pub mod random;
|
@ -1,46 +0,0 @@
|
|||||||
use crate::{errors::RunError, execute, parse, stack::StackVec, Stack};
|
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
pub fn args(memory: &mut StackVec, origin_stack: &mut Option<&mut Stack>, data: u16) {
|
|
||||||
if let Some(origin_stack) = origin_stack.as_mut() {
|
|
||||||
let mut data = data;
|
|
||||||
|
|
||||||
if data == 0 {
|
|
||||||
data = origin_stack.memory.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
for _ in 0..data {
|
|
||||||
memory.push(origin_stack.memory.pop());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
eprintln!("{}", RunError::RequestArgsInMainModule);
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exec(mut stack: &mut Stack, arg: String, mod_name: String) {
|
|
||||||
if mod_name == arg {
|
|
||||||
eprintln!("{}", RunError::ExecuteItself(arg.to_string()));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
let lines = fs::read_to_string(&arg).expect(&format!("{}", RunError::FailToReadFile));
|
|
||||||
let mut temp_stack = Stack::new(stack.memory.size());
|
|
||||||
|
|
||||||
parse(&mut temp_stack, &lines);
|
|
||||||
execute(&mut temp_stack, Some(&mut stack), arg.clone());
|
|
||||||
|
|
||||||
for item in temp_stack.memory.iter() {
|
|
||||||
stack.memory.push(*item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn call(stack: &mut Stack, mod_name: String) {
|
|
||||||
let len: u16 = stack.memory.pop();
|
|
||||||
let mut rev: String = String::new();
|
|
||||||
for _i in 0..len {
|
|
||||||
rev.push(stack.memory.pop() as u8 as char);
|
|
||||||
}
|
|
||||||
let tocall: String = rev.chars().rev().collect::<String>();
|
|
||||||
exec(stack, tocall, mod_name);
|
|
||||||
}
|
|
8
src/instructions/modulo.rs
Normal file
8
src/instructions/modulo.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn modulo(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(b % a);
|
||||||
|
}
|
8
src/instructions/mul.rs
Normal file
8
src/instructions/mul.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn mul(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(b * a);
|
||||||
|
}
|
14
src/instructions/nand.rs
Normal file
14
src/instructions/nand.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn nand(memory: &mut Vec<u16>) {
|
||||||
|
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
|
||||||
|
let c: u16 = if !(a && b) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
memory.push(c);
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
use crate::Stack;
|
|
||||||
use libloading::{Library, Symbol};
|
|
||||||
|
|
||||||
static mut STACKPTR: *mut Stack = 0 as *mut Stack;
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
unsafe extern fn stack_pop_callback() -> u16 {
|
|
||||||
(*STACKPTR).memory.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
unsafe extern fn stack_push_callback(data: u16) {
|
|
||||||
(*STACKPTR).memory.push(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
unsafe extern fn stack_len_callback() -> usize {
|
|
||||||
(*STACKPTR).memory.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub unsafe fn native(stack: &mut Stack, arg: String) {
|
|
||||||
STACKPTR = stack as *mut Stack;
|
|
||||||
#[cfg(not(target_family = "windows"))]
|
|
||||||
let libsuf: String = ".so".to_owned();
|
|
||||||
|
|
||||||
#[cfg(target_family = "windows")]
|
|
||||||
let libsuf: String = ".dll".to_owned();
|
|
||||||
|
|
||||||
let module = Library::new(format!("./{}{}", arg, libsuf)).unwrap();
|
|
||||||
// C libraries should use
|
|
||||||
// void (*labashka)(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size);
|
|
||||||
let func: Symbol<unsafe extern "C" fn(unsafe extern fn() -> u16, unsafe extern fn(u16), unsafe extern fn() -> usize, usize)> = module.get(b"labashka").unwrap();
|
|
||||||
func(stack_pop_callback,stack_push_callback,stack_len_callback, stack.memory.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
13
src/instructions/not.rs
Normal file
13
src/instructions/not.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn not(memory: &mut Vec<u16>) {
|
||||||
|
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
|
||||||
|
let c: u16 = if !a {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
memory.push(c);
|
||||||
|
}
|
14
src/instructions/or.rs
Normal file
14
src/instructions/or.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn or(memory: &mut Vec<u16>) {
|
||||||
|
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
|
||||||
|
let c: u16 = if a || b {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
memory.push(c);
|
||||||
|
}
|
11
src/instructions/pick.rs
Normal file
11
src/instructions/pick.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn pick(memory: &mut Vec<u16>, data: &u16) {
|
||||||
|
if *data > memory.len() as u16 {
|
||||||
|
eprintln!("{}", RunError::PickTooDeep);
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
let arg = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
memory.push(*memory.get(memory.len() - (arg as usize) - 1).expect(&format!("{}", RunError::PickOutOfBounds)));
|
||||||
|
}
|
@ -1,17 +0,0 @@
|
|||||||
#[cfg(target_family = "unix")]
|
|
||||||
use crate::stack::StackVec;
|
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
use std::fs::File;
|
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
pub fn _unix_random(memory: &mut StackVec) {
|
|
||||||
let mut rng = File::open("/dev/urandom").unwrap();
|
|
||||||
|
|
||||||
let mut buffer = [0u8; 2];
|
|
||||||
rng.read_exact(&mut buffer).unwrap();
|
|
||||||
|
|
||||||
let data = memory.pop();
|
|
||||||
memory.push((u16::from_ne_bytes(buffer)) % (data - 1));
|
|
||||||
}
|
|
3
src/instructions/pop.rs
Normal file
3
src/instructions/pop.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn pop(memory: &mut Vec<u16>) {
|
||||||
|
memory.pop();
|
||||||
|
}
|
3
src/instructions/print.rs
Normal file
3
src/instructions/print.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn print(arg: &String) {
|
||||||
|
print!("{}", arg);
|
||||||
|
}
|
3
src/instructions/println.rs
Normal file
3
src/instructions/println.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn println(arg: &String) {
|
||||||
|
println!("{}", arg);
|
||||||
|
}
|
3
src/instructions/push.rs
Normal file
3
src/instructions/push.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn push(memory: &mut Vec<u16>, data: &u16) {
|
||||||
|
memory.push(*data);
|
||||||
|
}
|
5
src/instructions/putc.rs
Normal file
5
src/instructions/putc.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn putc(memory: &mut Vec<u16>) {
|
||||||
|
print!("{}", memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) as u8 as char);
|
||||||
|
}
|
7
src/instructions/random.rs
Normal file
7
src/instructions/random.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
use fastrand;
|
||||||
|
use crate::RunError;
|
||||||
|
|
||||||
|
pub fn random(memory: &mut Vec<u16>) {
|
||||||
|
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
memory.push(fastrand::u16(..) % (*data - 1));
|
||||||
|
}
|
10
src/instructions/scan.rs
Normal file
10
src/instructions/scan.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn scan(memory: &mut Vec<u16>) {
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
std::io::stdin().read_line(&mut input).expect(&format!("{}", RunError::FailToReadLineFromConsole));
|
||||||
|
|
||||||
|
let num = input.trim().parse().expect(&format!("{}", RunError::InvalidInputUShortInt));
|
||||||
|
memory.push(num);
|
||||||
|
}
|
3
src/instructions/size.rs
Normal file
3
src/instructions/size.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub fn size(memory: &mut Vec<u16>) {
|
||||||
|
memory.push(memory.len() as u16);
|
||||||
|
}
|
@ -1,57 +0,0 @@
|
|||||||
use crate::{errors::RunError, stack::StackVec};
|
|
||||||
|
|
||||||
pub fn dump(memory: &mut StackVec) {
|
|
||||||
if memory.is_empty() {
|
|
||||||
println!("Stack is empty.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i, entry) in memory.iter().enumerate() {
|
|
||||||
println!("{0}: {1}", i, entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(memory: &mut StackVec) {
|
|
||||||
memory.push(memory.len() as u16);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn maxsize(memory: &mut StackVec) {
|
|
||||||
memory.push(memory.size() as u16);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expr(memory: &mut StackVec, arg: String) {
|
|
||||||
for code in arg.chars() {
|
|
||||||
match code {
|
|
||||||
'>' => memory.push(0),
|
|
||||||
'$' => { memory.pop(); },
|
|
||||||
':' => {
|
|
||||||
let data = memory.last().clone();
|
|
||||||
memory.push(data);
|
|
||||||
},
|
|
||||||
'\\' => {
|
|
||||||
let ax = memory.pop();
|
|
||||||
let ax2 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(ax);
|
|
||||||
memory.push(ax2);
|
|
||||||
}
|
|
||||||
'+' => {
|
|
||||||
let ax = memory.pop();
|
|
||||||
memory.push(ax + 1);
|
|
||||||
}
|
|
||||||
'-' => {
|
|
||||||
let ax = memory.pop();
|
|
||||||
memory.push(ax - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
eprintln!("{}", RunError::InvalidExpressionUnknownOperator(code.to_string()));
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exit(data: u16) {
|
|
||||||
std::process::exit(data as i32);
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
use crate::{errors::RunError, stack::StackVec};
|
|
||||||
use fastrand;
|
|
||||||
|
|
||||||
pub fn random(memory: &mut StackVec) {
|
|
||||||
let data = memory.pop();
|
|
||||||
memory.push(fastrand::u16(..) % (data - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rjmp(memory: &mut StackVec, labels: &mut [Option<i16>; 256], program_counter: &mut u16) {
|
|
||||||
let start = memory.pop() as usize;
|
|
||||||
let end = memory.pop() as usize;
|
|
||||||
let rand = fastrand::usize(start..end + 1);
|
|
||||||
|
|
||||||
*program_counter =
|
|
||||||
(labels[rand].expect(&format!("{}", RunError::UnknownLabel(rand as u16))) - 1) as u16;
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
use crate::{errors::RunError, stack::StackVec};
|
|
||||||
|
|
||||||
pub fn push(memory: &mut StackVec, data: u16) {
|
|
||||||
memory.push(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pop(memory: &mut StackVec) {
|
|
||||||
memory.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dup(memory: &mut StackVec) {
|
|
||||||
let data = memory.last();
|
|
||||||
memory.push(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn swap(memory: &mut StackVec) {
|
|
||||||
let a: u16 = memory.pop();
|
|
||||||
let b: u16 = memory.pop();
|
|
||||||
|
|
||||||
memory.push(a);
|
|
||||||
memory.push(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pick(memory: &mut StackVec, data: u16) {
|
|
||||||
if data > memory.len() as u16 {
|
|
||||||
eprintln!("{}", RunError::PickTooDeep);
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
let arg = memory.pop();
|
|
||||||
memory.push(
|
|
||||||
*memory
|
|
||||||
.get(memory.len() - (arg as usize) - 1)
|
|
||||||
.expect(&format!("{}", RunError::PickOutOfBounds)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn string(memory: &mut StackVec, arg: String) {
|
|
||||||
for i in arg.chars() {
|
|
||||||
memory.push(i as u16);
|
|
||||||
}
|
|
||||||
|
|
||||||
memory.push(arg.len() as u16);
|
|
||||||
}
|
|
8
src/instructions/sub.rs
Normal file
8
src/instructions/sub.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn sub(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(b - a);
|
||||||
|
}
|
9
src/instructions/swap.rs
Normal file
9
src/instructions/swap.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn swap(memory: &mut Vec<u16>) {
|
||||||
|
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||||
|
|
||||||
|
memory.push(a);
|
||||||
|
memory.push(b);
|
||||||
|
}
|
14
src/instructions/xor.rs
Normal file
14
src/instructions/xor.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use crate::errors::RunError;
|
||||||
|
|
||||||
|
pub fn xor(memory: &mut Vec<u16>) {
|
||||||
|
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||||
|
|
||||||
|
let c: u16 = if !a != !b {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
memory.push(c);
|
||||||
|
}
|
52
src/main.rs
52
src/main.rs
@ -1,18 +1,15 @@
|
|||||||
use std::{
|
use std::{fs, io::{BufRead, Write}};
|
||||||
fs,
|
|
||||||
io::{BufRead, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use errors::RunError;
|
use errors::RunError;
|
||||||
use execute::execute;
|
use execute::execute;
|
||||||
use parse::parse;
|
use parse::parse;
|
||||||
use stack::Stack;
|
use stack::Stack;
|
||||||
|
|
||||||
mod errors;
|
mod stack;
|
||||||
|
mod parse;
|
||||||
mod execute;
|
mod execute;
|
||||||
mod instructions;
|
mod instructions;
|
||||||
mod parse;
|
mod errors;
|
||||||
mod stack;
|
|
||||||
|
|
||||||
const HELP: &str = "\
|
const HELP: &str = "\
|
||||||
labast - Labaski interpreter written in Rust.
|
labast - Labaski interpreter written in Rust.
|
||||||
@ -20,7 +17,6 @@ labast - Labaski interpreter written in Rust.
|
|||||||
usage: labast [options] [ file.lb ]
|
usage: labast [options] [ file.lb ]
|
||||||
|
|
||||||
options:
|
options:
|
||||||
--stack-size sets stack max size (default: 256)
|
|
||||||
-h, --help prints help information
|
-h, --help prints help information
|
||||||
-v, --version prints version
|
-v, --version prints version
|
||||||
";
|
";
|
||||||
@ -34,7 +30,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut stack = Stack::new(args.stack_size);
|
let mut stack = Stack::new();
|
||||||
|
|
||||||
if args.input.is_none() {
|
if args.input.is_none() {
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
@ -50,32 +46,21 @@ fn main() {
|
|||||||
|
|
||||||
stdin.lock().read_line(&mut line).unwrap();
|
stdin.lock().read_line(&mut line).unwrap();
|
||||||
|
|
||||||
parse(&mut stack, line.trim());
|
parse(&mut stack, &line.trim());
|
||||||
execute(&mut stack, None, "inline".to_string());
|
execute(&mut stack, None, &"inline".to_string());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let input = &args.input.expect("error: no input file");
|
let input = &args.input.expect("error: no input file");
|
||||||
let lines = fs::read_to_string(&input).expect(&format!("{}", RunError::FailToReadFile));
|
let lines = fs::read_to_string(&input).expect(&format!("{}", RunError::FailToReadFile));
|
||||||
|
|
||||||
parse(&mut stack, &lines);
|
parse(&mut stack, &lines);
|
||||||
execute(
|
execute(&mut stack, None, &String::from(input.file_name().expect("Failed to read file name.").to_str().expect("Failed to convert file name to string")));
|
||||||
&mut stack,
|
|
||||||
None,
|
|
||||||
String::from(
|
|
||||||
input
|
|
||||||
.file_name()
|
|
||||||
.expect("Failed to read file name.")
|
|
||||||
.to_str()
|
|
||||||
.expect("Failed to convert file name to string"),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct AppArgs {
|
struct AppArgs {
|
||||||
input: Option<std::path::PathBuf>,
|
input: Option<std::path::PathBuf>,
|
||||||
stack_size: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_args() -> Result<AppArgs, pico_args::Error> {
|
fn parse_args() -> Result<AppArgs, pico_args::Error> {
|
||||||
@ -87,17 +72,11 @@ fn parse_args() -> Result<AppArgs, pico_args::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pargs.contains(["-v", "--version"]) {
|
if pargs.contains(["-v", "--version"]) {
|
||||||
println!(
|
println!("labast version {}", option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"));
|
||||||
"labast version {}",
|
|
||||||
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
|
|
||||||
);
|
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = AppArgs {
|
let args = AppArgs {
|
||||||
stack_size: pargs
|
|
||||||
.opt_value_from_fn("--stack-size", parse_stack_size)?
|
|
||||||
.unwrap_or(256),
|
|
||||||
input: pargs.opt_free_from_str()?,
|
input: pargs.opt_free_from_str()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -108,16 +87,3 @@ fn parse_args() -> Result<AppArgs, pico_args::Error> {
|
|||||||
|
|
||||||
return Ok(args);
|
return Ok(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_stack_size(s: &str) -> Result<usize, &'static str> {
|
|
||||||
let number = s
|
|
||||||
.parse()
|
|
||||||
.map_err(|_| "Argument for stack size is not a number");
|
|
||||||
|
|
||||||
if number > Ok(u16::MAX) {
|
|
||||||
eprintln!("Stack size cannot be more than 65535");
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(number?.into())
|
|
||||||
}
|
|
||||||
|
38
src/parse.rs
38
src/parse.rs
@ -1,7 +1,4 @@
|
|||||||
use crate::{
|
use crate::{errors::ParseError, stack::{Instruction, Stack}};
|
||||||
errors::ParseError,
|
|
||||||
stack::{Instruction, Stack},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn parse(stack: &mut Stack, file_content: &str) {
|
pub fn parse(stack: &mut Stack, file_content: &str) {
|
||||||
for (i, line) in file_content.lines().enumerate() {
|
for (i, line) in file_content.lines().enumerate() {
|
||||||
@ -16,40 +13,33 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
|
|||||||
inst_line = line.trim().chars().collect();
|
inst_line = line.trim().chars().collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
let command: [String; 2] = inst_line.split_once(char::is_whitespace).map_or_else(
|
let command: [String; 2] = inst_line.split_once(char::is_whitespace)
|
||||||
|| [inst_line.trim().to_string(), "".to_string()],
|
.map_or_else(|| [inst_line.trim().to_string(), "".to_string()],
|
||||||
|(first, second)| [first.trim().to_string(), second.trim().to_string()],
|
|(first, second)| [first.trim().to_string(), second.trim().to_string()]);
|
||||||
);
|
|
||||||
|
|
||||||
let name: String = command[0].clone();
|
let name: String;
|
||||||
let mut arg: String = String::new();
|
let mut arg: String = String::new();
|
||||||
let mut data: u16 = 0;
|
let mut data: u16 = 0;
|
||||||
|
|
||||||
|
name = command[0].clone();
|
||||||
|
|
||||||
if !command[1].is_empty() {
|
if !command[1].is_empty() {
|
||||||
if name.chars().nth(0) == Some('#') || name.chars().nth(1) == Some('#') {
|
if name.chars().nth(0) == Some('#') || name.chars().nth(1) == Some('#') {
|
||||||
let splited_data: Vec<String> =
|
let splited_data: Vec<String> = command[1].split_whitespace().map(String::from).collect();
|
||||||
command[1].split_whitespace().map(String::from).collect();
|
|
||||||
arg = splited_data.get(0).unwrap().clone();
|
arg = splited_data.get(0).unwrap().clone();
|
||||||
} else if name.chars().nth(0) == Some('$') || name.chars().nth(1) == Some('$') {
|
} else if name.chars().nth(0) == Some('$') || name.chars().nth(1) == Some('$') {
|
||||||
arg = command[1].clone();
|
arg = command[1].clone();
|
||||||
} else if let Ok(number) = command[1].parse::<i32>() {
|
} else if command[1] == "-1" { // required for
|
||||||
data = number as u16;
|
data = 65535; // _unix_random
|
||||||
|
} else if let Ok(number) = command[1].parse() {
|
||||||
|
data = number;
|
||||||
} else {
|
} else {
|
||||||
eprintln!(
|
eprintln!("{}", ParseError::ArgumentNotRequired(command[0].to_string(), i + 1, line.to_string()));
|
||||||
"{}",
|
|
||||||
ParseError::ArgumentNotRequired(
|
|
||||||
command[0].to_string(),
|
|
||||||
i + 1,
|
|
||||||
line.to_string()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if name.is_empty() {
|
let inst = Instruction { name, arg, data };
|
||||||
let inst = Instruction { name, arg, data, ispiped: false, isdrained: false };
|
|
||||||
stack.program.push(inst);
|
stack.program.push(inst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
62
src/stack.rs
62
src/stack.rs
@ -1,77 +1,25 @@
|
|||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use crate::errors::StackError;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Instruction {
|
pub struct Instruction {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub arg: String,
|
pub arg: String,
|
||||||
pub data: u16,
|
pub data: u16
|
||||||
pub ispiped: bool,
|
|
||||||
pub isdrained: bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct Stack {
|
pub struct Stack {
|
||||||
pub program: Vec<Instruction>,
|
pub program: Vec<Instruction>,
|
||||||
pub program_counter: u16,
|
pub program_counter: u16,
|
||||||
pub labels: [Option<i16>; 256],
|
pub labels: [Option<i16>; 256],
|
||||||
pub memory: StackVec,
|
pub memory: Vec<u16>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stack {
|
impl Stack {
|
||||||
pub fn new(stack_size: usize) -> Stack {
|
pub fn new() -> Stack {
|
||||||
return Stack {
|
return Stack {
|
||||||
program: Vec::new(),
|
program: Vec::new(),
|
||||||
program_counter: 0,
|
program_counter: 0,
|
||||||
labels: [None; 256],
|
labels: [None; 256],
|
||||||
memory: StackVec::new(stack_size),
|
memory: Vec::new()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StackVec {
|
|
||||||
data: Vec<u16>,
|
|
||||||
max_size: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StackVec {
|
|
||||||
fn new(max_size: usize) -> Self {
|
|
||||||
StackVec {
|
|
||||||
data: Vec::new(),
|
|
||||||
max_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(&self) -> usize {
|
|
||||||
self.max_size
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push(&mut self, item: u16) {
|
|
||||||
if self.data.len() + 1 > self.max_size {
|
|
||||||
panic!("{}", StackError::StackOverflow);
|
|
||||||
} else {
|
|
||||||
self.data.push(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pop(&mut self) -> u16 {
|
|
||||||
self.data
|
|
||||||
.pop()
|
|
||||||
.expect(&format!("{}", StackError::StackUnderflow))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn last(&mut self) -> u16 {
|
|
||||||
*self
|
|
||||||
.data
|
|
||||||
.last()
|
|
||||||
.expect(&format!("{}", StackError::StackUnderflow))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for StackVec {
|
|
||||||
type Target = Vec<u16>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
64
syntax-highlighting/notepad++.xml
Normal file
64
syntax-highlighting/notepad++.xml
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<NotepadPlus>
|
||||||
|
<UserLang name="Labashki" ext="lb" udlVersion="2.1">
|
||||||
|
<Settings>
|
||||||
|
<Global caseIgnored="no" allowFoldOfComments="no" foldCompact="yes" forcePureLC="0" decimalSeparator="0" />
|
||||||
|
<Prefix Keywords1="no" Keywords2="no" Keywords3="no" Keywords4="no" Keywords5="yes" Keywords6="no" Keywords7="no" Keywords8="no" />
|
||||||
|
</Settings>
|
||||||
|
<KeywordLists>
|
||||||
|
<Keywords name="Comments">00; 01 02 03 04</Keywords>
|
||||||
|
<Keywords name="Numbers, prefix1"></Keywords>
|
||||||
|
<Keywords name="Numbers, prefix2"></Keywords>
|
||||||
|
<Keywords name="Numbers, extras1"></Keywords>
|
||||||
|
<Keywords name="Numbers, extras2"></Keywords>
|
||||||
|
<Keywords name="Numbers, suffix1"></Keywords>
|
||||||
|
<Keywords name="Numbers, suffix2"></Keywords>
|
||||||
|
<Keywords name="Numbers, range"></Keywords>
|
||||||
|
<Keywords name="Operators1">^ |</Keywords>
|
||||||
|
<Keywords name="Operators2"></Keywords>
|
||||||
|
<Keywords name="Folders in code1, open"></Keywords>
|
||||||
|
<Keywords name="Folders in code1, middle"></Keywords>
|
||||||
|
<Keywords name="Folders in code1, close"></Keywords>
|
||||||
|
<Keywords name="Folders in code2, open">[</Keywords>
|
||||||
|
<Keywords name="Folders in code2, middle"></Keywords>
|
||||||
|
<Keywords name="Folders in code2, close">]</Keywords>
|
||||||
|
<Keywords name="Folders in comment, open"></Keywords>
|
||||||
|
<Keywords name="Folders in comment, middle"></Keywords>
|
||||||
|
<Keywords name="Folders in comment, close"></Keywords>
|
||||||
|
<Keywords name="Keywords1">#insert</Keywords>
|
||||||
|
<Keywords name="Keywords2">push pop dup swap pick add sub mul div mod putc getc scan meow args dump maxsize size quit exit _unix_random</Keywords>
|
||||||
|
<Keywords name="Keywords3">#print $print #exec #expr</Keywords>
|
||||||
|
<Keywords name="Keywords4">and or xor nand not band bor bxor bnand bnot</Keywords>
|
||||||
|
<Keywords name="Keywords5">> $ : \ + -</Keywords>
|
||||||
|
<Keywords name="Keywords6">jmp jnz jz kjnz kjz</Keywords>
|
||||||
|
<Keywords name="Keywords7">@</Keywords>
|
||||||
|
<Keywords name="Keywords8">nop</Keywords>
|
||||||
|
<Keywords name="Delimiters">00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23</Keywords>
|
||||||
|
</KeywordLists>
|
||||||
|
<Styles>
|
||||||
|
<WordsStyle name="DEFAULT" fgColor="F1F2F3" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="COMMENTS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="LINE COMMENTS" fgColor="57A64A" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="NUMBERS" fgColor="A6CEA8" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS1" fgColor="E67DDC" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS2" fgColor="559CD6" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS3" fgColor="D69D85" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS4" fgColor="FFFF80" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS5" fgColor="FF8080" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS6" fgColor="80FF00" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS7" fgColor="FF80FF" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="KEYWORDS8" fgColor="808080" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="OPERATORS" fgColor="00FFFF" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="FOLDER IN CODE1" fgColor="FF80FF" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="FOLDER IN CODE2" fgColor="FF8040" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="FOLDER IN COMMENT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS1" fgColor="FF8000" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS2" fgColor="00FF40" bgColor="FFFFFF" colorStyle="1" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS3" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS4" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
<WordsStyle name="DELIMITERS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
|
||||||
|
</Styles>
|
||||||
|
</UserLang>
|
||||||
|
</NotepadPlus>
|
@ -1,4 +0,0 @@
|
|||||||
@ 0
|
|
||||||
push -1
|
|
||||||
#native native_test
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
|||||||
// Build with
|
|
||||||
// rustc --crate-type cdylib native_test.rs -o native_test.so
|
|
||||||
// or
|
|
||||||
// rustc --crate-type cdylib native_test.rs -o native_test.dll
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
unsafe extern "C" fn labashka(pop: extern fn() -> u16, push: extern fn(u16), len: extern fn () -> usize, max: usize) {
|
|
||||||
println!("At {}, there's {}.", len(), pop());
|
|
||||||
push(12);
|
|
||||||
println!("Maximum size is {}.", max);
|
|
||||||
}
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user