연결됨 다시 연결 중 다시 연결 초 연결 실패 연결 일시 중지 복구 실패
I’ve started building a small language called SLang · Cover image

I’ve started building a small language called SLang

Public posts

A development log covering SLang’s syntax, value-flow calls, generated lexer and parser, and LLVM-based native builds.

Hello. This time, I am creating a small language called SmallLang.

It’s still really small. It’s a bit embarrassing to call it a language… ^^;

Still, the first grammar now feels a bit more like SmallLang.

getName: -> Text {
    "dimohy"
}

square: Int -> Int {
    it * it
}

main {
    getName -> name
    7 -> square -> num
    "Hello, {name}. square = {num}" -> print
}

The output is as follows.

Hello, dimohy. square = 49

And this code is compiled into LLVM IR, which is then linked into a Windows x64 native executable. The size of the generated exe is:

1,088 bytes

Huh... It’s still basic, but seeing it generate actual executable files is interesting.

Where is the grammar going?

From the beginning, I wanted a binding that can be read naturally without let or var.

name = "dimohy"

Here, recently, a -> syntax was added to make the value flow clearer.

getName -> name
7 -> square -> num
"Hello, {name}. square = {num}" -> print

getName -> name calls getName function and binds its result to name.

7 -> square -> num has a value of 7 flowing into the square function, and the result is bound to num.

If written in brackets, a format like num = square(7) is also possible, but in SmallLang, we prefer the approach where it’s clear where the value goes.

The function type also has the same directionality.

square: Int -> Int {
    it * it
}

In a function with just one input now, that input is implicitly received as it. Thus, it becomes it * it. It’s a somewhat bold choice, but for a small function, it reads quite simply.

Strings and numbers

String interpolation started as the smallest form.

"Hello, {name}. square = {num}"

For now, it’s just like {name}, where binding names are used. I’ll think about how to directly insert complex expressions into strings later. If we put everything in too quickly, the language becomes overly complicated right away.

The numbers currently support 64-bit integer literals as well as + and *. * comes before +, and both are left-assigned.

What's the interior like?

The compiler is being developed based on the latest .NET and C# Preview standards.

At first, I made it quickly, so the Lexer was included in Program.cs… Ah… I sorted it out right away. Now, I've divided it by role.

  • Cli
  • Lexing
  • Parsing
  • Syntax
  • Semantics
  • CodeGen
  • Tooling

Lexer and Parser are also not manually inserted, but generated using a source generator from a small rule file.

The Lexer rule is like this.

token Identifier = identifier
token String = quoted_string
token Number = number
token Plus = "+"
token Star = "*"
token Arrow = "->"
token Colon = ":"
token Equal = "="

The parser rules are also stored in a separate file.

rule FunctionDeclaration = Identifier Colon FunctionSignature LeftBrace NewLine* Expression NewLine* RightBrace
rule FunctionSignature = Arrow TypeName | TypeName Arrow TypeName
rule FlowExpression = AdditiveExpression (Arrow Path)*
rule AdditiveExpression = MultiplicativeExpression (Plus MultiplicativeExpression)*
rule MultiplicativeExpression = PrimaryExpression (Star PrimaryExpression)*

The Roslyn incremental source generator reads syntax/smalllang.lexer and syntax/smalllang.grammar, and generates Lexer, TokenKind, and Parser.

I wanted to separate the grammar representation from the C# implementation code. Since language creation is in progress, it would be a bit regrettable if the language grammar gets too entangled within the C# code.

How is it actually implemented?

The current pipeline is as follows.

SmallLangsource
-> generated lexer
-> generated parser
-> AST
-> semantic analysis
-> LLVM IR
-> clang object
-> lld-link exe

We don’t put LLVM in the repository. If there’s no build script, it is downloaded under .tools, and the tools and outputs are not committed to Git.

This sample may be collapsed into a single string at compilation time, but it is intentionally shown with a runtime function call.

The generated LLVM IR will have this format.

define internal i64 @smalllang_fn_square(i64 %it) #0 {
entry:
  %mul0 = mul nsw i64 %it, %it
  ret i64 %mul0
}

And on main, it is actually called this way.

%call4 = call i64 @slang_fn_square(i64 7)

The output is still just print. On the surface:

"Hello, {name}. square = {num}" -> print

However, internally, the string fragments are stored as UTF-8 static data, and integers are converted to decimal numbers at runtime and written directly as WriteFile. No CRT was used.

What remains

There is still much to finalize.

Type system, module system, memory model, cross-platform output, error handling, string escape, whether println…

It's quite depressing ^^;

Still, the first impression is good. Starting from a very minor syntax issue, it goes through tokenization, parsing, and passes through LLVM, all the way to the actual executable being created.

Here, I aim to gradually improve the grammar while maintaining the "simplicity".

How far can the smallest language be beautiful?

Let's burn it down… ^^

0 Comments

Comments: 0

Sign in to leave a comment.

No comments yet. Write the first one.

The server connection was interrupted. Reconnect ×