Skip to content

Instantly share code, notes, and snippets.

@tj
Created October 26, 2015 17:26
Show Gist options
  • Save tj/f7926cb3db01d6c0ca45 to your computer and use it in GitHub Desktop.
Save tj/f7926cb3db01d6c0ca45 to your computer and use it in GitHub Desktop.
{ type: 'Program',
body:
[ { type: 'FunctionDeclaration',
id: { type: 'Identifier', name: 'count' },
params:
[ { type: 'FormalParameter',
id: { type: 'Identifier', name: 'n' },
kind:
{ type: 'VariableType',
kind: { type: 'Identifier', name: 'int' } } } ],
kind: null,
body:
{ type: 'BlockStatement',
body:
[ { type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'printf' },
args:
[ { type: 'Literal', value: 'counting down from %d' },
{ type: 'Identifier', name: 'n' } ] } },
{ type: 'WhileStatement',
test:
{ type: 'BinaryExpression',
operator: '>',
left: { type: 'Identifier', name: 'n' },
right: { type: 'Literal', value: 0 } },
body:
{ type: 'BlockStatement',
body:
[ { type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'printf' },
args:
[ { type: 'Literal', value: '- %d\n' },
{ type: 'UpdateExpression',
operator: '--',
arg: { type: 'Identifier', name: 'n' },
prefix: false } ] } } ] } } ] } },
{ type: 'FunctionDeclaration',
id: { type: 'Identifier', name: 'main' },
params: [],
kind: { type: 'Identifier', name: 'int' },
body:
{ type: 'BlockStatement',
body:
[ { type: 'VariableDeclaration',
declarations:
[ { type: 'VariableDeclarator',
id: { type: 'Identifier', name: 'n' },
kind: null,
init: { type: 'Literal', value: 5 } } ] },
{ type: 'UnlessStatement',
test:
{ type: 'BinaryExpression',
operator: '>',
left: { type: 'Identifier', name: 'n' },
right: { type: 'Literal', value: 0 } },
consequent:
{ type: 'BlockStatement',
body:
[ { type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'printf' },
args: [ { type: 'Literal', value: 'error: n must be positive' } ] } },
{ type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'exit' },
args: [ { type: 'Literal', value: 1 } ] } } ] } },
{ type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'count' },
args: [ { type: 'Identifier', name: 'n' } ] } },
{ type: 'ReturnStatement', arg: { type: 'Literal', value: 0 } } ] } } ] }
fn count(n int) {
printf("counting down from %d", n)
for n > 0 {
printf("- %d\n", n--)
}
}
fn main() int {
let n = 5
unless n > 0 {
printf("error: n must be positive")
exit(1)
}
count(n)
return 0
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void
count (int n) {
printf("counting down from %d", n);
while (n > 0) {
printf("- %d\n", n--);
}
}
int
main () {
int n = 5;
if (!(n > 0)) {
printf("error: n must be positive");
exit(1);
}
count(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment