当前位置: 首页 > 软件库 > 开发工具 > 编译器 >

Command-Block-Assembly

Compile high-level code into Minecraft commands
授权协议 MIT License
开发语言 C/C++
所属分类 开发工具、 编译器
软件类型 开源软件
地区 不详
投 递 者 谢海阳
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Command Block Assembly

Command Block Assembly started off as a tool that compiled assembly instructionsinto Minecraft commands.

It has now grown into a much larger suite of tools for compiling source code into Minecraftcommands, therefore the toolchain as a whole is known as the Minecraft Compiler Collection (MCC).

Minecraft Compiler Collection

The Minecraft Compiler Collection (MCC) is an umbrella tool composed of multiple compilers in a toolchain tocompile high level code into Minecraft commands. It shares some similarities with the GNU Compiler Collection.

MCC is composed of the following components:

CBL Compiler

Command Block Language (CBL) is a C++ inspired programming language specifically designed for Minecraft commands.

CBL has been designed to abstract away from commands to the point where you don't need to consider the underlying mechanics.

The syntax is similar to C++ in a lot of ways. It also takes from Java's idea of having no pointers in the language.

Example

include "Text"

type point {
    int x;
    int y;

    constructor(int x, int y) {
        this.x = x;
        this.y = y;
    }

    inline point operator +(point other) {
        return point(this.x + other.x, this.y + other.y);
    }

    inline point operator *(int val) {
        return point(this.x * val, this.y * val);
    }

    inline void print();
}

inline void point::print() {
    Text output;
    output += "(";
    output.append_ref(this.x);
    output += ", ";
    output.append_ref(this.y);
    output += ")";
    output.send_to_all();
}

void main() {
    point p1(2, 4);
    point p2(5, 9);
    p1 += p2;
    p1 *= 2;
    p1.print();
}

This example demonstrates several language features of CBL, some of which should be familiar to C++ programmers.

Because everything in this example can be determined statically (at compile time), the output is just:

tellraw @a [{"text":"("},{"text":"14"},{"text":", "},{"text":"26"},{"text":")"}]

Documentation

See the CBL Wiki for more details on the CBL language.

Assembler

The assembler in MCC is the original assembler when this project was just Command Block Assembly.

Assembly Language

The assembly language is simple and has instructions similar to x86.

The language looks like the following:

#include foo.asm

; Useful comment

.my_const #1 ; A constant value
.my_ref my_const ; A constant reference

main:
    MOV #0x01, 0 ; Do the thing
    _loop: ADD my_const, 0
    JMP _loop

Example

The first example code written for CBA was an assembly program that prints the fibinacci sequence untilthe next value overflows:

.x 0x00
.y 0x01
.old_x 0x02
.counter 0x03

main:
    MOV #0, x
    MOV #1, y
    MOV #1, counter
    _loop:
    PRINT "fib(", counter, ") = ", x
    SYNC
    ADD #1, counter
    MOV x, old_x
    MOV y, x
    ADD old_x, y
    CMP #0, x
    JGE _loop ; if not >=0 then x has overflowed

Documentation

The instruction set and how write CBA programs is documented on the wiki.

C Compiler

MCC partially implements a C compiler, most language features are implemented and it includes a preprocessor.

The C compiler sits on top of the assembler - all C code is compiled into assembly which is then passed through MCC's assembler.

Example

Here is the same fibinacci sequence program but implemented in C:

#include <stdio.h>

int x;
int y;
int old_x;
int counter;

void main() {
    x = 0;
    y = 1;
    counter = 1;
    do {
        printf("fib(%d) = %d", counter++, x);
        sync;
        old_x = x;
        x = y;
        y += old_x;
    } while(x >= 0);
}

Documentation

The C compiler tries to stay as close as possible to the real C language, so documentation for most language features can be found elsewhere.

There is only one built-in type, int. Fundamentally, all data is stored in a scoreboard objectivewhich is a 32 bit signed integer.

The mclib.h filecontains several useful macros and definitions.

Browse the code in the examples directory to see working examplesof C code.

Command IR Compiler

Command IR is the intermediate representation used by MCC. All compilers above ultimately generate Command IR code.

It is designed to relate closely to Minecraft commands themselves but provide a level of abstraction and context whichenables optimizations among other things.

Command IR supports linking, which means it's possible to write code in CBL, C, ASM and IR directly and compile themtogether into one program.

Example

Here is a hello world program:

function hello {
    preamble {
        $all_players = selector a
        $message = text
        extern
    }

    compiletime {
        entry:
        text_append $message, "Hello, "
        text_append $message, "World!"
    }

    begin:
    text_send $message, $all_players
    ret
}

The output is a file hello.mcfunction:

tellraw @a [{"text":"Hello, "},{"text":"World!"}]

Documentation

Command IR's syntax, instruction reference and internal working is documented on the wiki.

Datapack Packer

Finally, to bring everything together and generate a datapack, MCC reads a "Datapack Definition" filewhich declares how to package the code into a datapack.

The file is a simple INI file declaring attributes such as the namespace.

To complete the fibinacci example, the datapack definition file fib.dpd is:

[Datapack]
namespace=fib
place location=0 56 0
description = An example datapack that prints the fibonacci sequence

Documentation

Currently there is only one section in a DPD file, the Datapack section.

In order to create a datapack, two values are required: the namespace and the place location

Option Description
namespace The namespace used for functions in the datapack
place location An absolute position in the world to place the utility command block. Specifiy as 3 space-separated numbers
spawn location A position in the world to spawn the global armorstand. Specifiy as 3 space-separated components. Relative positioning (~) is allowed
description Set the description of the datapack
generate cleanup Generate a function which will remove everything created by the datapack

MCC pipeline

There are 3 main stages to the compiler pipeline:

  1. Compiling: Converts source code into Command IR
  2. Linking: Merges one or more Command IR files into one master file
  3. Packing: Converts Command IR into Minecraft commands and creates a datapack from a datapack definition file

The MCC command takes a list of files, each code file goes through some number of transformations to end up as a Command IR object.

The linker merges each Command IR object into a single Command IR object. Any conflicts when merging will abort the compiler.

A datapack definition file is required for the packer. The packer takes the single Command IR object and generates the finalmcfunction files.

To get an idea of the hierarchy of components, here is a diagram:

            +---------------+
            |               |
            |  C Compiler   |
            |               |
+-----------+---------------+
|           |               |
|    CBL    |   Assembler   |
|           |               |
+-----------+---------------+
|                           |
|        Command IR         |
|                           |
+---------------------------+----+
|                                |
|      Command Abstraction       |       +--------------+
+--------------------------------+-------|   Datapack   |
|          Minecraft Commands            |  Definition  |
+----------------------------------------+--------------+
|                           Datapack                    |
+-------------------------------------------------------+

Running MCC

You will need to generate the standalone parsers (from Lark) using the ./build-parsers.sh script.If on Windows, run the python commands in that script from the root of this project.

The Lark python package needs to be installed, pip can be used on the requirements.txt file. It is recommended to use virtualenv.

Example: virtualenv env --python=python3 && source env/bin/activate && pip install -r requirements.txtOnce the parsers have been built, you don't technically need the virtual environment anymore. It can be deleted withdeactivate && rm -rf env/

Alternatively, you don't need to create the standalone parsers if you keep Lark available in the python environment.

MCC is implemented in python. Currently it is not bundled into an executable so it must be invoked using the python interpreter.

MCC is invoked by python mcc.py (If python 3 is not your default python command then run python3 mcc.py).

Command help text:

usage: mcc.py [-h] [-o outfile] [-E] [-c] [-S] [-dump-asm] [-O {0,1}]
              [-dump-ir] [-shared] [--dummy-datapack] [--stats]
              [--dump-commands] [--c-page-size SIZE]
              infile [infile ...]

Command line tool for the Minecraft Compiler Collection

positional arguments:
  infile              Input files

optional arguments:
  -h, --help          show this help message and exit

Output generation control:
  Options that control what stage in the output pipeline the compiler should
  stop at.

  -o outfile          Set the filename to write output to.
  -E                  Stop after running the preprocessor. Input files which
                      do not pass through a preprocessor are ignored.
  -c                  Compile source files, but do not link. An object file is
                      created for each source file
  -S                  Do not compile Command IR code. A Command IR file is
                      created for each non Command IR source file

C Compiler options:
  Options specific to the C compiler.

  -dump-asm           Print The generated ASM code to stdout. Compilation is
                      stopped after this point.

Optimization control:
  -O {0,1}            Control optimization level

Linker arguments:
  Arguments that control how the linker behaves.

  -dump-ir            Print Command IR textual representation to stdout after
                      linking objects, then exit

Packer arguments:
  Arguments that control how the packer behaves.

  -shared             Include a symbol table and other metadata to enable
                      dynamic linking of datapacks
  --dummy-datapack    Don't write an output datapack. This can be used for
                      debugging with --dump-commands
  --stats             Print statistics about the generated datapack
  --dump-commands     Dump the commands to stdout as they are written to the
                      datapack
  --c-page-size SIZE  Size of the memory page to generate if using the C
                      compiler

MCC will dispatch a different tool depending on what the file extension of each input file is:

  • .cbl Will compile the CBL source code file
  • .c Will compile the C source code file
  • .asm Will compile the assembly file
  • .ir Will read the Command IR file
  • .o Will load the pre-compiled Command IR object file
  • .dpd Will read the datapack definition file

Depending on the command line arguments, MCC will perform different tasks on each file. Some filesare ignored if they are not relevant for the desired action.

Examples

Compiling examples/fib.asm into a datapack fib.zip:

python mcc.py examples/fib.asm examples/fib.dpd

Compiling examples/hdd_driver.c into Command IR examples/hdd_driver.ir:

python mcc.py examples/hdd_driver.c -S

Compiling mylib.cbl into an object file, statically linking myprogram.cbl with the object and examples/fib.ir,then using mydatapack.dpd to create mysuperdatapack.zip:

python mcc.py mylib.cbl -c
python mcc.py mylib.o myprogram.cbl examples/fib.ir mydatapack.dpd -o mysuperdatapack.zip
 相关资料
  • Jasmine还允许开发人员跳过一个或多个测试用例。 这些技术可以在Spec level或Suite level 。 根据应用程序的不同,此块可分别称为Skipping Spec和Skipping Suite 。 在以下示例中,我们将学习如何使用“x”字符跳过特定的Spec或Suite 。 跳过规格 我们将在语句之前使用“x”修改前面的示例。 describe('This custom match

  • 命令模式在操作之间添加抽象级别,并包含一个调用这些操作的对象。 在此设计模式中,客户端创建一个命令对象,其中包含要执行的命令列表。 创建的命令对象实现特定的接口。 以下是命令模式的基本架构 - 如何实现命令模式? 我们现在将看到如何实现设计模式。 def demo(a,b,c): print 'a:',a print 'b:',b print 'c:',c class Comma

  • 本文向大家介绍block、inline、inline-block的区别?相关面试题,主要包含被问及block、inline、inline-block的区别?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: block元素会独占一行,多个block元素会各自新起一行。默认情况下,block元素宽度自动填满其父元素宽度。 block元素可以设置width,height属性。块级元素即使设置了宽度

  • 组件中传递的属性可以在块表达式中返回结果。 下表列出了使用块参数的不同方法 - S.No. BlockParam方式和描述 1 从具有yield的组件返回值 可以使用yield选项从组件返回值。 2 支持块和非块组件使用 您可以使用hasBlock属性支持单个组件使用块和非块组件。

  • 我实现了一个try-catch块。 我试图用一种特定的方式实现捕捉块,但是它不太好用。如果输入不是整数,它应该重复并返回到try块。它只工作一次,但更多。 你能给我一些帮助吗?非常感谢。

  • 描述 (Description) 块网格用于分割内容。 块网格可以合并到主网格中。 包括格式[size]-up-[n]类以更改不同屏幕或设备的所有列的大小。 例子 (Example) 以下示例演示了在基础中使用Block Grids - <!DOCTYPE html> <html> <head> <title>Foundation Template</title> <