Giter VIP home page Giter VIP logo

codetranslator's Introduction

Code Translator

A Code Translator for C8, CS146.

This translate converts loops or ifs into labels, goto and simple ifs. All code and examples are in translate.hs.

ghci translate.hs to run it.

Updated

Rewritten using Monad, the old version is trans-old.hs.

Examples:

Simple While Loop with Break

int i = 0;
while (i < 100) {
  printf("%d\n", i);
  if (i == 66) break;
  ++i;
}

Write the code above into Haskell:

s2 = [Exp "int i = 0", 
    While "i < 100" 
       [Exp "printf(\"%d\\n\", i)",
        If "i == 66" [Break] [], 
        Exp "++i"]]

After translation:

*Main> ts s2
int i = 0;
L0:;
if (!(i < 100)) goto L2;
print i;
if (i == 66) goto L1;
++i;
goto L0;
L2:;
L1:;

Perfect Numbers from 1 to 10000

int i = 1;
while (i <= 10000) {
  int acc = 0;
  for (int j = 1; j < i; ++j) {
    if (i % j == 0) acc += j;
  }
  if (i == acc) printf("%d\n", i);
  ++i;
}

Write the code above into Haskell:

s5 = [Exp "int i = 1", 
    While "i <= 10000" 
       [Exp "int acc = 0", 
        (For "int j = 1" "j < i" "++j" 
           [If "i % j == 0" [Exp "acc += j"] []]), 
        (If "i == acc" [Exp "printf(\"%d\\n\", i)"] []), 
        (Exp "++i")]]

After translation:

*Main> ts s5
int i = 1;
L0:;
if (!(i <= 10000)) goto L2;
int acc = 0;
int j = 1;
L3:;
if (!(j < i)) goto L6;
if (i % j == 0) acc += j;
L4:;
++j;
goto L3;
L6:;
L5:;
if (i == acc) printf("%d\n", i);
++i;
goto L0;
L2:;
L1:;

Switch

s4 = [Exp "int i = 0", 
    Switch "i" 
       [("0", [Exp "printf(\"%d = 0\\n\", i)"]), 
        ("1", [Exp "printf(\"%d = 1\\n\", i)"]), 
        ("2", [Exp "printf(\"%d = 2\\n\", i)"])] 
       [Exp "printf(\"other\\n\")"]]

into:

*Main> ts s4
int i = 0;
if (i == 0) goto L0;
if (i == 1) goto L2;
if (i == 2) goto L4;
printf("other\n");
goto L5;
L4:;
printf("%d = 2\n", i);
L5:;
goto L3;
L2:;
printf("%d = 1\n", i);
L3:;
goto L1;
L0:;
printf("%d = 0\n", i);
L1:;

Report A Problem

Feel free to report mistakes I made or give suggestions at GitHub Issue.

codetranslator's People

Contributors

noti0na1 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.