Compiler/Sample programs: Difference between revisions

no edit summary
(Additional samples)
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 10:
__TOC__
 
=={{header|Hello Worldworld/Text}}==
 
{| class="wikitable"
Line 20:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*
Hello world
*/
print("Hello, World!\n");
</syntaxhighlight>
</lang>
 
 
Line 77:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*
Show Ident and Integers
Line 83:
phoenix_number = 142857;
print(phoenix_number, "\n");
</syntaxhighlight>
</lang>
 
 
Line 154:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*
All lexical tokens - not syntactically correct, but that will
Line 177:
/* character literal */ '\\'
/* character literal */ ' '
</syntaxhighlight>
</lang>
 
 
Line 240:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*** test printing, embedded \n and comments with lots of '*' ***/
print(42);
print("\nHello World\nGood Bye\nok\n");
print("Print a slash n - \\n.\n");
</syntaxhighlight>
</lang>
 
 
Line 328:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
count = 1;
while (count < 10) {
Line 334:
count = count + 1;
}
</syntaxhighlight>
</lang>
 
 
Line 444:
count is: 8
count is: 9
</pre>
 
=={{header|100_doors}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
/* 100 Doors */
i = 1;
while (i * i <= 100) {
print("door ", i * i, " is open\n");
i = i + 1;
}
</syntaxhighlight>
 
 
| style="vertical-align:top" |
<b><pre>
lex ..\100doors.t
2 1 Identifier i
2 3 Op_assign
2 5 Integer 1
2 6 Semicolon
3 1 Keyword_while
3 7 LeftParen
3 8 Identifier i
3 10 Op_multiply
3 12 Identifier i
3 14 Op_lessequal
3 17 Integer 100
3 20 RightParen
3 22 LeftBrace
4 5 Keyword_print
4 10 LeftParen
4 11 String "door "
4 18 Comma
4 20 Identifier i
4 22 Op_multiply
4 24 Identifier i
4 25 Comma
4 27 String " is open\n"
4 39 RightParen
4 40 Semicolon
5 5 Identifier i
5 7 Op_assign
5 9 Identifier i
5 11 Op_add
5 13 Integer 1
5 14 Semicolon
6 1 RightBrace
7 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
lex ..\100doors.t | parse
Sequence
Sequence
;
Assign
Identifier i
Integer 1
While
LessEqual
Multiply
Identifier i
Identifier i
Integer 100
Sequence
Sequence
;
Sequence
Sequence
Sequence
;
Prts
String "door "
;
Prti
Multiply
Identifier i
Identifier i
;
Prts
String " is open\n"
;
Assign
Identifier i
Add
Identifier i
Integer 1
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
lex ..\100doors.t | parse | gen
Datasize: 1 Strings: 2
"door "
" is open\n"
0 push 1
5 store [0]
10 fetch [0]
15 fetch [0]
20 mul
21 push 100
26 le
27 jz (49) 77
32 push 0
37 prts
38 fetch [0]
43 fetch [0]
48 mul
49 prti
50 push 1
55 prts
56 fetch [0]
61 push 1
66 add
67 store [0]
72 jmp (-63) 10
77 halt
</pre></b>
|}
 
;And the output is:
<pre>
lex ..\100doors.t | parse | gen | vm
door 1 is open
door 4 is open
door 9 is open
door 16 is open
door 25 is open
door 36 is open
door 49 is open
door 64 is open
door 81 is open
door 100 is open
</pre>
 
Line 456 ⟶ 601:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
a = (-1 * ((-1 * (5 * 15)) / 10));
print(a, "\n");
Line 463 ⟶ 608:
print(-b, "\n");
print(-(1), "\n");
</syntaxhighlight>
</lang>
 
 
Line 663 ⟶ 808:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
print(---------------------------------+++5, "\n");
print(((((((((3 + 2) * ((((((2))))))))))))), "\n");
 
if (1) { if (1) { if (1) { if (1) { if (1) { print(15, "\n"); } } } } }
</syntaxhighlight>
</lang>
 
 
Line 1,008 ⟶ 1,153:
 
 
=={{header|GCDGreatest common divisor}}==
{| class="wikitable"
|-
Line 1,017 ⟶ 1,162:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/* Compute the gcd of 1071, 1029: 21 */
 
Line 1,029 ⟶ 1,174:
}
print(a);
</syntaxhighlight>
</lang>
 
 
Line 1,150 ⟶ 1,295:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/* 12 factorial is 479001600 */
 
Line 1,161 ⟶ 1,306:
}
print(result);
</syntaxhighlight>
</lang>
 
 
Line 1,277 ⟶ 1,422:
|}
 
=={{header|Fibonacci sequence}}==
 
{| class="wikitable"
Line 1,287 ⟶ 1,432:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/* fibonacci of 44 is 701408733 */
 
Line 1,301 ⟶ 1,446:
}
print(w, "\n");
</syntaxhighlight>
</lang>
 
 
Line 1,357 ⟶ 1,502:
13 14 RightParen
13 15 Semicolon
1514 1 End_of_input
</pre></b>
 
Line 1,465 ⟶ 1,610:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/* FizzBuzz */
i = 1;
Line 1,481 ⟶ 1,626:
i = i + 1;
}
</syntaxhighlight>
</lang>
 
 
Line 1,693 ⟶ 1,838:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/* 99 bottles */
bottles = 99;
while (bottles >= 0) {
print(bottles, " bottles of beer on the wall\n");
print(bottles, " bottles of beer\n");
print("Take one down, pass it around\n");
bottles = bottles - 1;
print(bottles, " bottles of beer on the wall\n\n");
bottles = bottles - 1;
}
</syntaxhighlight>
</lang>
 
 
Line 1,715 ⟶ 1,860:
3 7 LeftParen
3 8 Identifier bottles
3 16 Op_greaterequalOp_greater
3 1918 Integer 0
3 2019 RightParen
3 2221 LeftBrace
4 5 Keyword_print
4 10 LeftParen
Line 1,738 ⟶ 1,883:
6 44 RightParen
6 45 Semicolon
7 5 Keyword_printIdentifier bottles
7 1013 LeftParenOp_assign
7 1115 Identifier bottles
7 1823 CommaOp_subtract
7 2025 StringInteger " bottles of beer on the wall\n\n"1
7 5426 RightParenSemicolon
78 55 Semicolon5 Keyword_print
8 10 5 Identifier bottlesLeftParen
8 1311 Op_assignIdentifier bottles
8 1518 Identifier bottlesComma
8 20 String " bottles of beer on the wall\n\n"
8 23 Op_subtract
8 2554 Integer 1RightParen
8 2655 Semicolon
9 1 RightBrace
10 1 End_of_input
Line 1,797 ⟶ 1,942:
String "Take one down, pass it around\n"
;
Assign
Identifier bottles
Subtract
Identifier bottles
Integer 1
Sequence
Sequence
Line 1,806 ⟶ 1,956:
String " bottles of beer on the wall\n\n"
;
Assign
Identifier bottles
Subtract
Identifier bottles
Integer 1
</pre></b>
 
Line 1,838 ⟶ 1,983:
55 prts
56 fetch [0]
61 prtipush 1
6266 push 3sub
67 prtsstore [0]
6872 fetch [0]
7377 push 1prti
78 subpush 3
7983 store [0]prts
84 jmp (-75) 10
89 halt
Line 1,857 ⟶ 2,002:
99 bottles of beer
Take one down, pass it around
9998 bottles of beer on the wall
 
98 bottles of beer on the wall
98 bottles of beer
Take one down, pass it around
98 bottles of beer on the wall
 
97 bottles of beer on the wall
97 bottles of beer
Take one down, pass it around
97 bottles of beer on the wall
Line 1,874 ⟶ 2,014:
2 bottles of beer
Take one down, pass it around
21 bottles of beer on the wall
 
1 bottles of beer on the wall
1 bottles of beer
Take one down, pass it around
10 bottles of beer on the wall
 
0 bottles of beer on the wall
0 bottles of beer
Take one down, pass it around
0 bottles of beer on the wall
</pre>
 
Line 1,897 ⟶ 2,033:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*
Simple prime number generator
Line 1,918 ⟶ 2,054:
}
print("Total primes found: ", count, "\n");
</syntaxhighlight>
</lang>
 
 
Line 2,232 ⟶ 2,368:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
{
/*
Line 2,275 ⟶ 2,411:
}
}
</syntaxhighlight>
</lang>