Project Overview
This project implements a programming language inspired by the one-liners of Arnold Schwarzenegger. It parses code written in this language and generates Java bytecode that can be executed.
Tech Stack
- Scala: The primary programming language.
- ASM: A Java bytecode manipulation framework used for generating
.classfiles. - Parboiled-scala: A library for writing parsers.
- ScalaTest: A testing framework.
- FreeTTS: A speech synthesis library, likely for the
-declaimoption.
High-Level Architecture
The project follows a typical compiler structure:
- Parser (
ArnoldParser): Takes the ArnoldC source code as input and transforms it into an Abstract Syntax Tree (AST). The AST is composed of variousAstNodeimplementations, such asStatementNode,ExpressionNode, andOperandNode. - AST (
org.arnoldc.ast.*): Represents the structure of the parsed ArnoldC code. Nodes likePrintNode,AssignVariableNode,MethodNode, andMainMethodNodedefine the language's constructs. - Code Generator (
ArnoldGenerator,ArnoldParser): Traverses the AST and generates Java bytecode using the ASM library. Thegeneratemethods within eachAstNodeare responsible for emitting the appropriate bytecode instructions. - Class Loader (
ArnoldGenerator,ByteCodeExecutor): Loads the generated bytecode as a Java class. - Executor (
Executor): Runs the compiled Java class, specifically executing itsmainmethod. - Declaimer (
Declaimer- not fully shown, but referenced): Likely processes the AST to produce a human-readable representation or perform some analysis.
Entry Points
org.arnoldc.ArnoldC.main(args: Array[String]): This is the main entry point for the command-line interface. It handles parsing command-line arguments (like-runor-declaim), reading the ArnoldC source file, invoking theArnoldGeneratorto produce bytecode, writing the.classfile, and then either executing the compiled code viaExecutoror processing it withDeclaimer.org.arnoldc.ArnoldGenerator.generate(arnoldCode: String, filename: String): This method orchestrates the parsing and bytecode generation process. It creates anArnoldParserinstance, parses the code into anRootNode(the root of the AST), and then calls thegenerateByteCodemethod on theRootNode.org.arnoldc.Executor.execute(className: String): This method is responsible for loading and running the generated Java bytecode. It uses a customClassLoaderto load the class and then invokes itsmainmethod.
The typical flow for running an ArnoldC program is: ArnoldC reads the source file -> ArnoldParser builds AST -> ArnoldGenerator generates bytecode -> FileOutputStream writes .class file -> Executor loads and runs the .class file.
Key Concepts
- Abstract Syntax Tree (AST): The core data structure representing the parsed ArnoldC code. Developers need to understand how different language constructs are mapped to
AstNodesubclasses (e.g.,PrintNodeforTALK TO THE HAND,AssignVariableNodeforGET TO THE CHOPPER).- Refer to:
src/main/scala/org/arnoldc/ast/AstNode.scalaand its subclasses.
- Refer to:
- Bytecode Generation: The process of translating AST nodes into Java bytecode instructions using the ASM library. Each
AstNodehas ageneratemethod responsible for this.- Refer to:
src/main/scala/org/arnoldc/ArnoldGenerator.scalaand thegeneratemethods in AST nodes likesrc/main/scala/org/arnoldc/ast/PrintNode.scala.
- Refer to:
- Symbol Table (
org.arnoldc.SymbolTable): Used to keep track of variables, their scopes, and method information during parsing and code generation. It's crucial for managing variable addresses and method signatures.- Refer to:
src/main/scala/org/arnoldc/SymbolTable.scala(not provided, but its usage is evident in AST nodes).
- Refer to:
- Main Method Structure: ArnoldC programs have a defined entry point,
IT'S SHOWTIMEandYOU HAVE BEEN TERMINATED, which corresponds to theMainMethodNode.- Refer to:
src/main/scala/org/arnoldc/ast/MainMethodNode.scalaandsrc/main/scala/org/arnoldc/ArnoldC.scala.
- Refer to:
- Keywords and Mappings: The project maps Arnold Schwarzenegger's catchphrases to programming language constructs. Understanding these mappings is key to writing ArnoldC code.
- Refer to:
README.mdfor the keyword list.
- Refer to:
