Compiler/Sample programs: Difference between revisions

no edit summary
(Fix bug in C solution)
No edit summary
 
(3 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;
Line 1,703 ⟶ 1,848:
print(bottles, " bottles of beer on the wall\n\n");
}
</syntaxhighlight>
</lang>
 
 
Line 1,888 ⟶ 2,033:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
/*
Simple prime number generator
Line 1,909 ⟶ 2,054:
}
print("Total primes found: ", count, "\n");
</syntaxhighlight>
</lang>
 
 
Line 2,223 ⟶ 2,368:
|-
| style="vertical-align:top" |
<syntaxhighlight lang="c">
<lang c>
{
/*
Line 2,266 ⟶ 2,411:
}
}
</syntaxhighlight>
</lang>