← Dolaima← Docs indexR原文中文

R

A vector language, close to desktop R in style.

How to run it on your phone

vml run examples/r/test/sysinfo.r

Compiling takes a while (one to two minutes for C, a few seconds for scripting languages). Once the program is running, the gamepad is at the bottom of the screen - the D-pad and the four action buttons are all there. Tap the back arrow in the top-right corner to return to the Shell.

What to know before you write

Examples

FileWhat it shows
test/catch.rCatch the blocks
test/file_io.rReading and writing files
test/parserexpf_demo.rCalling a shared-library expression parser
test/sysinfo.rDevice information

Also in this folder: demo/demo_std.r, test/libs.r, tty/demo_tty.r, ui/demo_ui.r


The rest of this page is taken straight from the VML source (third_party/vml/VMLPrepares/RCompiler/): the README covers what this front end supports and how to compile it, and the language reference covers the syntax itself. When upstream changes, regenerating this page brings it up to date.

Language reference

R language compiler specification

Version: v1.0 | Date: 2026-07-06 | Revised by: Shenzhen Tanso Intelligent Technology Co., Ltd.

Metadata

ItemDescription
StandardR 4.x subset (S language compatible)
Language birth year1993
Completeness~96%
File extension.r
Compiler entry pointRCompiler.RCompiler.Compile()
Plugin registrationRCompilerPlugin : CompilerPluginExBase
Language categorystatistical analysis language

Overview

R is a programming language and environment for statistical computing and graphics. This compiler implements the core syntax subset of R, supporting vectorized data operations, statistical functions, control flow and user-defined functions. The compilation output is VML assembly, which can be further translated to 16 target architectures.

The compiler architecture follows the standard VML front end pattern:

R source (.r) → Lexer (Lexer.cs) → Token stream
              → Parser (Parser.cs) → AST (ASTNode.cs)
              → CodeGenerator (CodeGenerator*.cs) → VML program

The entry file RCompiler.cs orchestrates the whole compilation pipeline, including preprocessing, lexing, parsing, code generation and standard library linking.

Supported features

Data types
TypeR representationInternal representationDescription
numeric42, 3.14, 1.5e-10int / floatIntegers and floats. In R, integer can take an L suffix (such as 42L); the L is stripped during parsing
character"hello", 'world'string (data section)String literals, supporting three delimiters: double quote, single quote and backtick
logicalTRUE, FALSEint (1 / 0)Booleans map to integers
NULLNULLint (0)Empty value; R0 is set to zero
NANAint (int.MinValue)Missing value, represented by a sentinel value
InfInffloat (float.PositiveInfinity)Infinity

Note: in the current implementation, numeric integer and float are not strictly distinguished at run time. Scientific notation (such as 1.5e-10, 2E+3) is fully supported.

Assignment operators

R supports three assignment forms, in descending order of precedence:

# 1. Left assignment (recommended style)
x <- 42
y <- c(1, 2, 3)

# 2. Equals assignment (function-argument style)
x = 42

# 3. Super assignment (assign into the parent environment)
x <<- 42
Arithmetic operators
OperatorSyntaxDescription
Additiona + bAdd numeric values
Subtractiona - bSubtract numeric values
Multiplicationa * bMultiply numeric values
Divisiona / bFloating-point division
Exponentiationa ^ bRaise to a power
Moduloa %% bInteger remainder (note: it must be two % characters)
Comparison operators
OperatorSyntaxDescription
Equala == bEquality comparison
Not equala != bInequality comparison
Less thana < bLess-than comparison
Greater thana > bGreater-than comparison
Less than or equala <= bLess than or equal to
Greater than or equala >= bGreater than or equal to

A comparison returns the integer 1 (TRUE) or 0 (FALSE).

Logical operators
OperatorSyntaxDescription
Logical ANDa & bShort-circuit evaluation (mapped to &&)
Logical ORa | bShort-circuit evaluation (mapped to ||)
Logical NOT!aUnary negation

Note: the current implementation does not distinguish the vectorized &/| from the short-circuit &&/||; all logical operations use short-circuit evaluation. R0 = 0 counts as FALSE, non-zero counts as TRUE.

Functions
# Function definition
add <- function(a, b) {
    return(a + b)
}

# Function with no parameters
greet <- function() {
    return("Hello")
}

# Implicit return: the value of the last expression in the body is the return value
square <- function(x) {
    x * x
}
Control flow
if / else
if (x > 0) {
    print("positive")
} else {
    print("non-positive")
}

# else if chain (parsed as nesting)
if (x > 0) {
    print("positive")
} else if (x < 0) {
    print("negative")
} else {
    print("zero")
}

The condition expression is evaluated and compared with 0: CMP R0, 0; JE. Non-zero is true.

while loop
i <- 1
while (i <= 10) {
    print(i)
    i <- i + 1
}

A pre-test loop: when the condition is false the loop body is skipped.

for loop
for (i in 1:5) {
    print(i)
}

for (x in c(10, 20, 30)) {
    print(x)
}
repeat loop
repeat {
    # Must contain a break condition, otherwise it is an infinite loop
    if (condition) {
        break
    }
}

An infinite loop; it must be exited explicitly with break.

break and next
# break: exit the innermost loop
for (i in 1:10) {
    if (i > 5) {
        break
    }
}

# next: skip the current iteration and go to the next one
for (i in 1:10) {
    if (i %% 2 == 0) {
        next
    }
    print(i)  # prints odd numbers only
}
Vectors
# c() constructor
x <- c(1, 2, 3, 4, 5)
y <- c(TRUE, FALSE, TRUE)
z <- c()  # empty vector, returns NULL

# Range operator
1:10    # integer sequence, parsed as a special call
Indexing
x <- c(10, 20, 30, 40)
a <- x[1]   # 10 (R indexing starts at 1)
b <- x[3]   # 30
Comments
# This is a single-line comment
x <- 1  # end-of-line comment

Only single-line comments starting with # are supported. Multi-line comments are not supported.

Scientific notation
x <- 1.5e-10   # 1.5 × 10^-10
y <- 2.0E+5    # 200000
z <- 3e8        # 300000000

The lexer fully parses scientific notation: mantissa, decimal point, e/E, optional sign, exponent.

Identifier rules
source() import
source("lib/utils.r")

The source() call is extracted from the source with a regular expression (parsed before the preprocessing stage) and links the contents of the given .r file into the current program. This is a compile-time mechanism, not a run-time function call.

Expression precedence table

Listed from highest to lowest precedence (a smaller number means higher precedence):

PrecedenceOperatorsAssociativityDescription
1() [] $ : f()leftGrouping, indexing, member access, range, function call
2- (unary) ! + (unary)rightUnary minus, logical NOT, unary plus
3^rightExponentiation
4* / %%leftMultiply, divide, modulo
5+ -leftAdd, subtract
6== != < > <= >=leftComparison operators
7&leftLogical AND
8|leftLogical OR
9<- <<- =rightAssignment

Predefined macros

The compiler injects the following macros during the preprocessing stage (Preprocessor):

Macro nameTypical valueDescription
__VML__1Indicates the code is being compiled in the VML toolchain
__VML_VERSION__"1.65.32"VML toolchain version number
__R__1Indicates the language being compiled is R
__DATE__"Jun 02 2026"Compilation date
__TIME__"15:30:00"Compilation time

These macros can be used through the preprocessor directives #ifdef, #ifndef, #if and so on, enabling conditional compilation.

Limitations and unsupported features

The current implementation is 🟢 production-ready level. The following core R language features are not supported yet:

Object system
Data structures
Functional programming
Formula interface
Vectorized operations
Packages and libraries
Other

Compiler implementation details

File structure
VMLPrepares/RCompiler/
├── Token.cs                      # Token type definitions and value carrier
├── Lexer.cs                      # Lexer (inherits LexerBase)
├── ASTNode.cs                    # Abstract syntax tree node definitions
├── Parser.cs                     # Recursive-descent parser
├── CodeGenerator.cs              # Code generation entry point
├── CodeGenerator.Expressions.cs  # Expression code generation
├── CodeGenerator.Statements.cs   # Statement code generation
├── RCompiler.cs                  # Compiler entry point and pipeline orchestration
├── RCompilerPlugin.cs            # Plugin registration
├── Program.cs                    # CLI entry point
└── RCompiler.csproj              # Project file
Register conventions

Follows the standard VML calling convention:

RegisterPurpose
R0accumulator / function return value
R12frame pointer (BP)
R13stack pointer (SP)
R15return address (RA)
Memory model
Function calling convention
String handling
Standard output

The current compiler has no built-in print() implementation. Output has to go through VML SYSCALLs or BIOS calls, or by linking the printf function from the C shared library.

Typical R to VML mappings
R constructVML instruction sequence
x <- 10MOVE R0, 10; MOVE [R12-x], R0
a + bMOVE R0, [R12-a]; MOVE R0, [R12-b]; ADD R0, R0, R0
if (x) { ... }MOVE R0, [R12-x]; CMP R0, 0; JE else_label; ...; LABEL else_label
while (x) { ... }LABEL while_start; MOVE R0, [R12-x]; CMP R0, 0; JE wend; ...; JMP while_start; LABEL wend
for (i in c(1,2,3)) { ... }index initialization + condition check loop + loading the sequence element value on every iteration
return(x)MOVE R0, [R12-x]; POP R12; RET
f(x, y)MOVE R0, [R12-y]; PUSH R0; MOVE R0, [R12-x]; PUSH R0; CALL func_f
v[i]compute base address + (i-1)*4, indirect load
c(1, 2, 3)SYSCALL 2 (malloc 12) + store elements in turn + restore the base address into R0

🆕 String encoding (v1.65.19)

This language compiler uses the VML string system indirectly through the shared library (Lib/shared/).

DirectiveWidthEncodingC type
.string8-bitUTF-8char*
.wstring16-bitUTF-16LEwchar_t*
.ustring32-bitUTF-32LEchar32_t*

MCU mode (default): string output is UTF-8 (.string) OS mode: the encoding can be determined through the VML_WSTRING macro

The shared library already provides wide-string conversion functions (wchar.h/uchar.h) that each language compiler can use as needed.

Compiler README

R compiler

Path: VMLPrepares/RCompiler/ Completeness: ~96% | 🟢 production ready Standard library: Lib/r/

Features

Compile modes

MCU mode (default, --mode mcu)

MCU mode is tuned for microcontroller / bare-metal environments.

Skipped:

Kept:

OS mode (--mode os)

OS mode supports the full set of language features.

RAM level

Usage

dotnet run --project VMLTool -- input.r -o output.vml

Tests