Project: KCL (Provisioning / Automation & Configuration)
File: 4.architecture.md

Although KCL is a Domain Specific Language (DSL), it is not much different from compilers for general-purpose languages. The main difference is that KCL provides more relevant syntax semantics for the problems it solves. Compilation begins when a user writes a KCL source program in text and invokes the `kcl` command on it. The bootstrap phase initializes the compiler environment by setting up the required contexts, loading the configuration files, and preparing the necessary resources to process the input KCL files. Lexing, or lexical analysis, is the process of converting a sequence of characters into a sequence of tokens. A lexer, also known as a tokenizer, scans the source code to produce these tokens. Parsing takes the tokens produced by the lexer and attempts to construct an Abstract Syntax Tree (AST) that represents the hierarchical syntactic structure of the source code. Resolving involves analyzing the AST to understand the relationships between different parts of the code. This includes identifying the scope of variables, resolving semantic names, and ensuring that references are correctly linked. The compiling phase translates the resolved AST into an intermediate representation (IR) that is optimized for the subsequent phases. The IR is a lower-level representation of the program that is closer to machine code. After compilation, the evaluating phase executes the program using the IR including AST and LLVM IR. During this phase, the compiler can evaluate expressions, compute values, and perform optimizations that are runtime-specific. At runtime, the KCL environment executes the compiled IR. This involves various runtime behaviors such as value calculations, error handling, and invoking system libraries. Values are the runtime representations of the entities defined in the KCL code. They can include primitives like integers and strings, as well as complex data structures. The standard libraries provide pre-defined functions and resources that the user can invoke in KCL programs. These libraries provide a wide array of functionality to interact with the system and perform complex tasks. Language tools are additional utilities that support language features, such as syntax highlighting, linting, formatting, and refactoring. The IDE extension provides developers with tools to write KCL code efficiently within an Integrated Development Environment (IDE). The Language Server Protocol (LSP) is used to offer capabilities like code completion, error detection, and hover information. KCL provides APIs and SDKs for multiple programming languages, allowing developers to interact with KCL programs from different technology stacks. | Term | Meaning | | -- | -- | | AST | The abstract syntax tree produced by the `kclvm_ast` crate; reflects user syntax very closely. | | Lexer | A tool that converts the input code into a stream of tokens for parsing. | | Parser | A tool that constructs an AST from a stream of tokens. | | Namer | The compiler component responsible for assigning unique names to language entities and ensuring they are used consistently throughout the program. | | Resolver | Part of the compiler that resolves ambiguities and links references to their corresponding declarations. | | Intermediate Representation (IR) | An intermediate code representation generated during the compilation process, designed to optimize subsequent code generation steps or direct execution. | | Value | In KCL, this usually refers to the result of a computation or any entity that can be expressed in code. | | Type System | A set of rules and checking processes used by the compiler to determine the types of various expressions and variables. |