/LocalizedSwiftCompiler

This is a demo compiler describing the basic idea behind localizing the compiler diagnose messages

Primary LanguageSwiftMIT LicenseMIT

LocalizedSwiftCompiler

Disclamer: I don't own the full project and it's originally created by @apbendi. I however, implemented the demo for localizing the diagnostic messages for my GSoC 2020 proposal.

You can view the original repo here.

Implementation

For localizing the diagnostic messages as an initial approach I created a JSON file that contains messages and for every message it has its own ID and languages that it's translated to.

JSON file:

{
  "diagnosticMessages": {
    "firstMessageID": {
      "languages": {
        "en": "[Error] Expecting a different entry",
        "fr": "[Erreur] Attendre une entrée différente"
      }
    }
  }
}

I then parsed the JSON file with Swift:

    // Error ID
    let errorID = "firstMessageID"
    
    // Chose your language
    let local = "fr"
    
    // The finished error message
    var localizedErrorMessage = ""
    
    guard currentToken.type == type else {
      
      // Parse the JSON file
      DiagnosticMessages.diagnoseMessage { messageJson in
        guard let jMessage = messageJson else { return }
        do {
          if let json = try JSONSerialization.jsonObject(with: jMessage, options: []) as? [String: Any] {
            if let messages = json["diagnosticMessages"] as? [String: Any] {
              if let localizedMessage = messages[errorID] as? [String: Any] {
                if let foo = localizedMessage["languages"] as? [String: Any] {
                  localizedErrorMessage = foo[local] as! String
                }
              }
            }
          }
        } catch {
          print(error.localizedDescription)
        }
      }
      
      // Printing the error message
      print(localizedErrorMessage)
      
      
      exit(EX_DATAERR)
    }

Testing

To run the test file you will need to build the compiler first by ./build.sh then you can run the test file by ./runbitsy samples/fail.bitsy

Screenshot

You can change your language in bitsy-swift/Parser




Original README.md

BitsySwift

BitsySwift is a compiler for the Bitsy language implemented in Swift. It is currently the canonical implementation of Bitsy.

Bitsy

Bitsy is a programming language which aims to be the best language to implement when building your first compiler or interpreter. It is a resource for programmers learning about language implementation.

To learn more about Bitsy or to try implementing it yourself, in your favorite language, check out the runnable, test based language specification, BitsySpec.

You can read more about the motivation behind creating Bitsy on the ScopeLift Blog.

I spoke about creating Bitsy and implementing it in Swift at several conferences in 2016. You can watch the talk on YouTube.

Requirements

This version of BitsySwift has been tested with:

  • macOS 10.14 (Mojave) or later
  • Xcode 11.3
  • Swift 5.0

Linux support is currently limited by Swift Foundation but should come eventually.

Installation

To 'install' the compiler, simply clone and build the repository. You must have Xcode and the xcodebuild utility installed.

git clone https://github.com/apbendi/bitsy-swift.git
cd bitsy-swift
./build.sh

Usage

Once built, you can use the runbitsy script to conveniently build and immediately run any .bitsy file.

./runbitsy samples/collatz.bitsy # Print the Collatz sequence for 7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

Note: The runbitsy script currently hangs for Bitsy programs which accept user input, though the binaries themselves run fine. Any bash experts know why?

Alternatively, you may directly use the bitsy-swift command line utility directly for additional options:

bin/bitsy-swift --help
Usage: bin/bitsy-swift [options]
  -v, --version:
      Print the version of bitsy-swift
  -h, --help:
      Display bitsy-swift usage
  -c, --read-cli:
      Read Bitsy code from the command line, terminated by a '.'
  -o, --output:
      Specify a name for the binary output
  -e, --emit-cli:
      Emit intermediate compilation to command line
  -r, --run-delete:
      Immediately run and delete the compiled binary
  -i, --retain-intermediate:
      Retain results of intermediate representation

Resources

While Bitsy has been created partially in response to a perceived lack of approachable resources for learning language implementation, there are still some good places to start.

Contributing

Contributions of all types are welcome! Open an issue, create a pull request, or just ask a question. The only requirement is that you be respectful of others.

Please checkout the BitsySpec repo and join the discussion to codify version 1.0 of the Bitsy language specification.