Coder Social home page Coder Social logo

mcbasic's Introduction

Linux pipeline status Windows pipeline status

MCBasic

MCBasic is a statically typed scripting language with a mixture of C syntax and Python features. It compiles to a datapack for Minecraft 1.21 and above.

This project is still WIP and is not fit for actual use.

Progress

Re-implementation

Due to the latest changes in the Minecraft datapack format and the new data-driven features, I decided to rewrite the compiler from scratch. In order to allow the community to contribute to the project, I decided to change the language to Kotlin, which is based on Java, so Minecraft modders can easily understand the code.

Contributing

Feel free to contribute to this project by opening an issue or a pull request. Optimization suggestions are also welcome, as I'm still learning about compilers.

To avoid duplicated work, please submit an issue before star

License

This project is licensed under the GNU General Public License v3.0. See the LICENSE file for more details.

mcbasic's People

Contributors

kolterdyx avatar

Watchers

 avatar  avatar

mcbasic's Issues

Decimal/Floating point number arithmetic

This issue is here to discuss decimal number arithmetic (floating/fixed point numbers) implementation.

Current implementation is whacky at best. It uses fixed point numbers and it has trouble with big numbers because it has to give up digits for decimal values. Addition and subtraction work fine, but multiplication and division cause a lot of trouble since either one or both of the numbers in the operation have to be scaled up or down by 10 to the power of however many digits of precision we have, which causes even more overflow issues. The only good thing is that it's almost as efficient as integer arithmetic (because it technically IS integer arithmetic). The implementation was severely limited by the fact that commands can't return floating point values, which means I couldn't store actual float/double values in storage, and then scale them up or down when needed. This is sadly working as intended so I doubt I'll be able to fix this without a huge overhaul implementing my very own data type.

Support for floating point arithmetic

The current arithmetic operators' implementation uses scoreboards for operating on numbers. scoreboards only support integers, which means that floating point numbers will be rounded before performing any operation.

As far as I know, the only floating point operation available is the scale parameter in the execute store storage <storage> <path> <type> <scale> command. This allows for floating point multiplication up to double precission, but that is about the only direct path available. Division could be accomplished by multiplying by a less than 1 scalar, but that just moves the problem further because we still need to figure out how to compute 1/x somehow without knowing the value of x at compile time.

Off the top of my head, a plausible solution would be to multiply the number by a constant order of magnitude, perform an integer operation, and then divide by the inverse of the constant. It would look soomething like this:

PRECISION = 1000
INV_PRECISION = 0.001
x = 3.14159265359
y = 1.76

# We are going to compute x/y

# scale only the numerator up
num = x * PRECISION # 3145.9265359
den = y
# perform integer operation, with the scoreboard command
tmp = num // den # 1787
# scale the result by the inverse of our precision value, which as we know at compile time, we can just hardcode
result = tmp * INV_PRECISION # 1.787

This kinda works, but has the issue that you can only have as much precision as you shift digits left. If your desired decimal precision is x, you would have to scale up and then down numbers by a factor of 10^x, which limits a lot our precision and can cause trouble with large numbers for large values of x.

Any other ideas are welcome

While loops

In general, loops are not exactly possible with Minecraft functions. You could use a recursive function, but that will be limited to the maxCommandChainLength gamerule, which by default is 2^16 or 65536. There is no command equivalent to the JMP instruction commonly found in ASM instruction sets, so we can only execute stuff linearly. The only loop we have is the tick function, which runs since the world is loaded, forever, under all circumstances.

We can use that to create some sort of scheduling system. Functions that contain loops would be split up, with each loop being a "sub" function of the original one. Then we replace the while loop in the original function with some instruction that tells the scheduler to run a function until a condition is met.

We would have to create such a function so that it takes as parameters all the necesary variables from the parent scope, and the CALL counter should not be increased in loops. Then we could maybe add the name of this subfunction to a queue, and the scheduler would run the first function in the queue every tick until a condition was no longer met, and then it would remove the function from the queue, and move on to the next.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.