Navigation
Outline:
Last modification
2025-09-04
This is smol web. If you don't like it, fix your browser.

Results#

Result are pretty good, stats here:

 5x 5
 1x 7
 1x 8
 1x 10
 1x 15
 1x 18
 1x 20
 3x 24
22x 25

Assignment#

Deadline: January 4th, 2019

Let’s create a very simple programming language called S# that works as follows:

Your task: Write a transpiler of S# to C, i.e. a program that reads an S# program and outputs a program in C that does the same thing. You may also transpile to C++, since the languages are almost identical from this perspective — choose whichever you want.

The input S# program is passed into standard input and terminated by standard EOF.

The following rules apply:

Extras:

Examples#

S# program:

test u v {u+v+u*v}
fact x { x * if (x>1) {fact(x-1)} {1}}
main {
  write(test(read(),
             read()));
  write ( fact ( 5 ));
  0
}

may be translated to following C program:

#include <stdio.h>
#include <stdint.h> //for uint64_t
typedef uint64_t u;
u read() {u x; scanf("%lu", &x); return x;}
u test(u a, u b) { return (a+b)+(a*b); }
u fact(u x) { return x * (x>1?fact(x-1):1); }
int main() {
    printf("%lu\n", test(read(),read()));
    printf("%lu\n", fact(5));
    return 0;
}

(Note: you may use any renaming of variables and function names you want that satisfies the assignment conditions. In fact, your program output may be COMPLETELY different from this one, as long as it represents the same program.)

Another S# program:

fun x {write(x); if(x>0) {fun(x-1)} {0} }
main {fun(10)}

May be translated to following C program:

#include <iostream>
#include <cstdint>
uint64_t f1(uint64_t p1) { std::cout << p1 << std::endl; return (p1>0)?f1(p1-1):0; }
int main() { return f1((10)); }

The following S# program is invalid and should report an error (for any of the 7 different reasons):

func x1 x1 { func2(x2,x1) }
Main x {func(1,2,3)-}
Corner cases#

Notice that the following is a valid S# program that should output 3 and 4.

main {write({write(3);4})}

Precious hint: , has two different meanings in C.