Code Generation

CPCG: A Cross-Paradigm Code Generator

My graduate software development project centred around cross-paradigm code generation. The completed project may be found in my Github repository.

Tasks:

  • Analyzed standard programs in cross-paradigm languages to develop an expressive design language (DSL)
  • Constructed this Haskell code generator to demonstrate the existence of abstract algorithms, a language of design, and a shared core across paradigms
  • Generated programming languages: Java, C, Lambda-Prolog, Haskell, Lua, Lisp, C#, and Scala

Abstract

The goal of this project is to demonstrate the existence of a language of design that bridges the gap between abstract algorithms and explicit code. When used to describe distinct concepts or thought patterns for algorithms, the four programming paradigms – functional, logic, object-oriented (OO), and imperative – appear unrelated initially. They are, however, able to express certain common concepts, albeit in different ways. The design decisions made between the specifications of abstract algorithms and their concrete implementations may be encoded in a code generator using this tangible language of design.

The Cross-Paradigm Code Generator (CPCG) is designed as a tool to generate code, allowing programmers to solve problems at the level of abstraction of the problem domain. Two domain specific languages (DSLs) were developed to incorporate the core ideas of this project; that is, the concepts and semantic characteristics of all the paradigms (referred to as the internal language in this project) and the choices behind the design of algorithms (referred to as the design language). The CPCG uses the internal DSL along with supplemental Haskell modules and data structures representing a series of languages (Java, Lua, Haskell, Prolog, C#, Lisp, Lua, Prolog, and Scala) to implement ASTs.


The generate Function

generate :: [Char] -> [Char] -> [D.Choices] -> IO ()

  • The first parameter (string) specifies the folder path in which the source file will generate
  • The second parameter (string) specifies the algorithm to generate; currently, the only ones available are: quicksort, fibonacci, factorial, exponent, palindrome, greatest common divisor (gcd) and least common multiple (lcm), maximum, and hello
  • The third parameter (array) specifies the design decisions for the algorithm, including but not limited to: pivot choice (head, middle, or last), loop (iterative or recursive), language, data structure (array or list), and paradigm
  • The resulting code is generated in a source file in the path from the first parameter, or into the same folder as Main.hs if no path is provided
Advertisements