Professional Documents
Culture Documents
Faculdade de Computao
Universidade Federal de Uberlndia
28 de novembro de 2017
Lista de Listagens
1.1 nano1.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 nano1.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 nano1.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 nano2.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 nano2.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 nano2.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7 nano3.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 nano3.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.9 nano1.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.10 nano4.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.11 nano1.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.12 nano1.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.13 nano5.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.14 nano5.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.15 nano1.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.16 nano6.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.17 nano6.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.18 nano6.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.19 nano7.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.20 nano7.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.21 nano7.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.22 nano8.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.23 nano8.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.24 nano8.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.25 teste1_9.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.26 teste1_9.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.27 teste1_9.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.28 nano10.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.29 nano10.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.30 nano10.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.31 nano11.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.32 nano11.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.33 nano11.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.34 nano12.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.35 nano12.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.36 nano12.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.37 micro01.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.38 micro01.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.39 micro01.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.40 micro02.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2
1.41 micro02.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.42 micro02.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.43 micro03.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.44 micro03.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.45 micro03.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.46 micro04.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.47 micro04.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.48 micro04.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.49 micro05.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.50 micro05.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.51 micro05.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.52 micro06.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.53 micro06.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.54 micro06.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.55 micro07.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.56 micro02.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.57 micro07.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.58 micro08.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.59 micro08.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.60 micro08.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
1.61 micro09.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
1.62 micro09.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1.63 micro09.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
1.64 micro10.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
1.65 micro10.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
1.66 micro10.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
1.67 micro11.pas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.68 micro11.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
1.69 micro11.il . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.1 lexico.mll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.1 sintatico.mly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2 ast.ml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.3 tast.ml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
4.1 sast.ml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.2 semantico.ml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.1 interprete.ml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Sumrio
1 Introduo 5
1.1 Linha de comando . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Linguagem Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Cdigos de programas em Pascal . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Analisador Lxico 60
3 Analisador Sinttico 63
4 Analisador Semntico 69
5 Interprete 78
4
Captulo 1
Introduo
5
1.2
Para fazer a compilao do cdigo fonte para o arquivo executvel usado o seguinte
comando no terminal Linux:
> mcs /out:nomeExecutavel.exe arquivoCodigoFonte.cs
add.ovf: retira os dois elementos do topo da pilha e coloca o resultado no topo, verifi-
cando overflow.
and: retira os dois elementos do topo da pilha, faz a operao and bit a bit e coloca o
resultado no topo da pilha.
bge: pula para o endereo se o topo da pilha menor que o seu antecesso.
bgt: pular para o endereo se o segundo elemento da pilha maior que o topo.
div: diviso.
newarr: cria um array com o tipo definido onde seu tamanho est no topo da pilha.
rem: computa o resto da diviso do elemento abaixo do topo, pelo topo, e empilha o
resultado.
xor: executaa a operao XOR(bit a bit) entre dois elementos da pilha colocando seu
resultado na mesma.
Para introduzir a listagem do cdigo no documento existem pelo menos duas formas
bsicas, ambas usando o pacote listings:
1. Nano1
Listagem 1.1: nano1.pas
1 PROGRAM NANO1;
2 BEGIN
3 END.
9 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..
WrapNonEx
10 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) //
ceptionThrows.
11
12 .hash algorithm 0x00008004
13 .ver 0:0:0:0
14 }
15 .module nano1.exe // GUID = {5ED09DB1-FA20-4D85-8F38-87FF39FA9BE5}
16
17
18 .class private auto ansi beforefieldinit nano1
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano1::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 1 (0x1)
41 .maxstack 8
42 IL_0000: ret
43 } // end of method nano1::Main
44
45 } // end of class nano1
2. Nano2
Listagem 1.4: nano2.pas
1 PROGRAM NANO2;
2 VAR
3 n: integer;
4
5 BEGIN
6 END.
3. Nano3
Listagem 1.7: nano3.pas
1 PROGRAM NANO3;
2 VAR
3 n: integer;
1.3
4
5 BEGIN
6 n := 1;
7 END.
41 .maxstack 1
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.1
45 IL_0001: stloc.0
46 IL_0002: ret
47 } // end of method nano3::Main
48
49 } // end of class nano3
4. Nano4
Listagem 1.10: nano4.pas
1 PROGRAM NANO4;
2 VAR
3 n: integer;
4
5 BEGIN
6 n := 1 + 2;
7 END.
5. Nano5
Listagem 1.13: nano5.pas
1 PROGRAM NANO5;
2 VAR
3 n: integer;
4
5 BEGIN
6 n := 2;
7 write(n);
8 END.
17
18 .class private auto ansi beforefieldinit nano5
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano5::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 9 (0x9)
41 .maxstack 1
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.2
45 IL_0001: stloc.0
46 IL_0002: ldloc.0
47 IL_0003: call void class [mscorlib]System.Console::Write(int32)
48 IL_0008: ret
49 } // end of method nano5::Main
50
51 } // end of class nano5
6. Nano6
Listagem 1.16: nano6.pas
1 PROGRAM NANO6;
2 VAR
3 n: integer;
1.3
4
5 BEGIN
6 n := 1 - 2;
7 write(n);
8 END.
3 class nano5 {
4 public static void Main(){
5 int n;
6 n = 1 - 2;
7 Console.Write(n);
8 }
9 }
17
18 .class private auto ansi beforefieldinit nano5
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano5::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
1.3
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 9 (0x9)
41 .maxstack 1
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.m1
45 IL_0001: stloc.0
46 IL_0002: ldloc.0
47 IL_0003: call void class [mscorlib]System.Console::Write(int32)
48 IL_0008: ret
49 } // end of method nano5::Main
50
51 } // end of class nano5
7. Nano7
Listagem 1.19: nano7.pas
1 PROGRAM NANO7;
2 VAR
3 n: integer;
4
5 BEGIN
6 n := 1;
7 if ( n = 1) then
8 begin
9 write(n);
10 end;
11 END.
10 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) //
ceptionThrows.
11
12 .hash algorithm 0x00008004
13 .ver 0:0:0:0
14 }
15 .module nano7.exe // GUID = {F3A7240D-AC0D-4B6E-A4F3-49D6B8F53EB8}
16
17
18 .class private auto ansi beforefieldinit nano7
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano7::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 16 (0x10)
41 .maxstack 2
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.1
45 IL_0001: stloc.0
46 IL_0002: ldloc.0
47 IL_0003: ldc.i4.1
48 IL_0004: bne.un IL_000f
49
50 IL_0009: ldloc.0
51 IL_000a: call void class [mscorlib]System.Console::Write(int32)
52 IL_000f: ret
53 } // end of method nano7::Main
54
8. Nano8
Listagem 1.22: nano8.pas
1 PROGRAM NANO8;
2 VAR
3 n: integer;
4
5 BEGIN
6 n := 1;
7 if ( n = 1) then
1.3
8 begin
9 write(n);
10 end;
11 else
12 begin
13 write(0);
14 end;
15 END.
17
18 .class private auto ansi beforefieldinit nano8
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
1.3
31 IL_0006: ret
32 } // end of method nano8::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 27 (0x1b)
41 .maxstack 2
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.1
45 IL_0001: stloc.0
46 IL_0002: ldloc.0
47 IL_0003: ldc.i4.1
48 IL_0004: bne.un IL_0014
49
50 IL_0009: ldloc.0
51 IL_000a: call void class [mscorlib]System.Console::Write(int32)
52 IL_000f: br IL_001a
53
54 IL_0014: ldc.i4.0
55 IL_0015: call void class [mscorlib]System.Console::Write(int32)
56 IL_001a: ret
57 } // end of method nano8::Main
58
9. Teste1_9
Listagem 1.25: teste1_9.pas
1 PROGRAM TESTE1_9;
2 VAR
3 n: real;
4
5 BEGIN
6 n := 1 + 1 / 2;
7 if ( n = 1) then
8 begin
9 write(n);
10 end;
11 else
12 begin
13 write(0);
14 end;
15 END.
8 Console.Write(n);
9 else
10 Console.Write(0);
11 }
12 }
49
50 IL_0009: ldloc.0
51 IL_000a: call void class [mscorlib]System.Console::Write(int32)
52 IL_000f: br IL_001a
53
54 IL_0014: ldc.i4.0
55 IL_0015: call void class [mscorlib]System.Console::Write(int32)
56 IL_001a: ret
57 } // end of method teste1_9::Main
58
10. Nano10
Listagem 1.28: nano10.pas
1 PROGRAM NANO10;
2 VAR
3 n,m: integer;
4
5 BEGIN
6 n := 1;
7 m := 2;
8 if ( n = m ) then
9 begin
10 write(n);
11 end;
12 else
13 begin
14 write(0);
15 end;
16 END.
7 {
8 .custom instance void class [mscorlib]System.Runtime.
CompilerServices.RuntimeCompatibilityAttribute::'.ctor'() = (
9 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..
WrapNonEx
10 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) //
ceptionThrows.
11
12 .hash algorithm 0x00008004
13 .ver 0:0:0:0
14 }
15 .module nano10.exe // GUID = {CEB6B741-839A-4CCC-BBBB-33314E97341A}
16
17
18 .class private auto ansi beforefieldinit nano10
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano10::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 29 (0x1d)
41 .maxstack 2
42 .locals init (
43 int32 V_0,
44 int32 V_1)
45 IL_0000: ldc.i4.1
46 IL_0001: stloc.0
47 IL_0002: ldc.i4.2
48 IL_0003: stloc.1
49 IL_0004: ldloc.0
50 IL_0005: ldloc.1
51 IL_0006: bne.un IL_0016
52
53 IL_000b: ldloc.0
54 IL_000c: call void class [mscorlib]System.Console::Write(int32)
55 IL_0011: br IL_001c
56
57 IL_0016: ldc.i4.0
58 IL_0017: call void class [mscorlib]System.Console::Write(int32)
59 IL_001c: ret
60 } // end of method nano10::Main
61
11. Nano11
Listagem 1.31: nano11.pas
1 PROGRAM NANO11;
2 VAR
3 n,m,x: integer;
4
5 BEGIN
6 n := 1;
7 m := 2;
8 x := 5;
9 while ( x > n ) do
10 begin
11 n := n + m;
12 write(n);
13 end;
14
15 END.
17
18 .class private auto ansi beforefieldinit nano11
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method nano11::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 29 (0x1d)
41 .maxstack 2
42 .locals init (
43 int32 V_0,
44 int32 V_1,
45 int32 V_2)
46 IL_0000: ldc.i4.1
47 IL_0001: stloc.0
48 IL_0002: ldc.i4.2
49 IL_0003: stloc.1
50 IL_0004: ldc.i4.5
51 IL_0005: stloc.2
52 IL_0006: br IL_0015
53
54 IL_000b: ldloc.0
55 IL_000c: ldloc.1
56 IL_000d: add
57 IL_000e: stloc.0
58 IL_000f: ldloc.0
59 IL_0010: call void class [mscorlib]System.Console::Write(int32)
60 IL_0015: ldloc.2
61 IL_0016: ldloc.0
62 IL_0017: bgt IL_000b
63
64 IL_001c: ret
65 } // end of method nano11::Main
66
67 } // end of class nano11
12. Nano12
Listagem 1.34: nano12.pas
1 PROGRAM NANO12;
2 VAR
3 n,m,x: integer;
1.3
4
5 BEGIN
6 n := 1;
7 m := 2;
8 x := 5;
9 while ( x > n ) do
10 begin
11 if ( n = m ) then
12 begin
13 write(n);
14 end;
15 else
16 begin
17 write(0);
18 end;
19 x := x - 1;
20 end;
21
22 END.
14 }
15 .module nano12.exe // GUID = {47B286E8-BC25-4510-B8F3-7840D64B7D28}
16
17
54 IL_000b: ldloc.0
55 IL_000c: ldloc.1
56 IL_000d: bne.un IL_001d
57
58 IL_0012: ldloc.0
59 IL_0013: call void class [mscorlib]System.Console::Write(int32)
60 IL_0018: br IL_0023
61
62 IL_001d: ldc.i4.0
63 IL_001e: call void class [mscorlib]System.Console::Write(int32)
64 IL_0023: ldloc.2
65 IL_0024: ldc.i4.1
66 IL_0025: sub
67 IL_0026: stloc.2
68 IL_0027: ldloc.2
69 IL_0028: ldloc.0
70 IL_0029: bgt IL_000b
71
72 IL_002e: ret
1.3
13. Micro01
Listagem 1.37: micro01.pas
1 PROGRAM MICRO01;
2 VAR
3 cel, fahr : real;
4
5 BEGIN
6 writeln('Tabela de converso: Celsius -> Fahrenhiet');
7 write('Digite a temperatura em Celsius: ');
8 readln(cel);
9 fahr := (9*cel+160)/5;
10 writeln('A nova temperatura : ', fahr, ' F');
11
12 END.
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method micro01::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 90 (0x5a)
41 .maxstack 3
42 .locals init (
43 float64 V_0,
44 float64 V_1)
45 IL_0000: ldstr bytearray (
46 54 00 61 00 62 00 65 00 6c 00 61 00 20 00 64 00 // T.a.b.e.l.a. .d
.
47 65 00 20 00 63 00 6f 00 6e 00 76 00 65 00 72 00 // e. .c.o.n.v.e.r
.
48 73 00 e3 00 6f 00 3a 00 20 00 43 00 65 00 6c 00 // s...o.:. .C.e.l
.
49 73 00 69 00 75 00 73 00 20 00 2d 00 3e 00 20 00 // s.i.u.s. .-.>.
.
50 46 00 61 00 68 00 72 00 65 00 6e 00 68 00 65 00 // F.a.h.r.e.n.h.e
.
51 69 00 74 00 01 ) // i.t..
52
69 65 00 6d 00 70 00 65 00 72 00 61 00 74 00 75 00 // e.m.p.e.r.a.t.u
.
70 72 00 61 00 20 00 e9 00 20 00 01 ) // r.a. ... ..
71
72 IL_0044: ldloc.1
73 IL_0045: box [mscorlib]System.Double
74 IL_004a: ldstr " F"
75 IL_004f: call string string::Concat(object, object, object)
76 IL_0054: call void class [mscorlib]System.Console::WriteLine(
string)
77 IL_0059: ret
78 } // end of method micro01::Main
79
80 } // end of class micro01
14. Micro02
Listagem 1.40: micro02.pas
1 PROGRAM MICRO02;
2 VAR
3 num1, num2 : integer;
4
5 BEGIN
6 write('Digite o primeiro nmero: ');
7 readln(num1);
8 write('Digeite o segundo nmero: ');
9 readln(num2);
10 if ( num1 > num2 ) then
11 begin
12 write('O primeiro nmero ', num1, ' maior que o segundo ',
num2);
13 end;
14 else
15 begin
16 write('O segundo nmero ', num2, ' maior que o primeiro ',
num1);
17 end;
18
19 END.
3 class micro02 {
4 public static void Main(){
5 int num1, num2;
6 Console.Write("Digite o primeiro nmero: ");
7 num1 = Convert.ToInt32(Console.ReadLine());
8 Console.Write("Digite o segundo nmero: ");
9 num2 = Convert.ToInt32(Console.ReadLine());
10 if ( num1 > num2)
11 Console.Write("O primeiro nmero " + num1 + " maior que o
segundo " + num2);
12 else
13 Console.Write("O segundo nmero " + num2 + " maior que o
primeiro" + num2);
14 }
1.3
15 }
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method micro02::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 155 (0x9b)
41 .maxstack 4
42 .locals init (
43 int32 V_0,
44 int32 V_1)
45 IL_0000: ldstr bytearray (
46 44 00 69 00 67 00 69 00 74 00 65 00 20 00 6f 00 // D.i.g.i.t.e. .o
.
47 20 00 70 00 72 00 69 00 6d 00 65 00 69 00 72 00 // .p.r.i.m.e.i.r
.
48 6f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // o. .n...m.e.r.o
.
49 3a 00 20 00 01 ) // :. ..
1.3
50
51 IL_0005: call void class [mscorlib]System.Console::Write(string)
52 IL_000a: call string class [mscorlib]System.Console::ReadLine()
53 IL_000f: call int32 class [mscorlib]System.Convert::ToInt32(string
)
54 IL_0014: stloc.0
55 IL_0015: ldstr bytearray (
56 44 00 69 00 67 00 69 00 74 00 65 00 20 00 6f 00 // D.i.g.i.t.e. .o
.
57 20 00 73 00 65 00 67 00 75 00 6e 00 64 00 6f 00 // .s.e.g.u.n.d.o
.
58 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 3a 00 // .n...m.e.r.o
.:.
59 20 00 01 ) // ..
60
61 IL_001a: call void class [mscorlib]System.Console::Write(string)
62 IL_001f: call string class [mscorlib]System.Console::ReadLine()
63 IL_0024: call int32 class [mscorlib]System.Convert::ToInt32(string
)
64 IL_0029: stloc.1
65 IL_002a: ldloc.0
66 IL_002b: ldloc.1
67 IL_002c: ble IL_0068
68
69 IL_0031: ldc.i4.4
70 IL_0032: newarr [mscorlib]System.Object
71 IL_0037: dup
72 IL_0038: ldc.i4.0
73 IL_0039: ldstr bytearray (
74 4f 00 20 00 70 00 72 00 69 00 6d 00 65 00 69 00 // O. .p.r.i.m.e.i
.
75 72 00 6f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 // r.o. .n...m.e.r
.
76 6f 00 20 00 01 ) // o. ..
77
78 IL_003e: stelem.ref
79 IL_003f: dup
80 IL_0040: ldc.i4.1
81 IL_0041: ldloc.0
82 IL_0042: box [mscorlib]System.Int32
83 IL_0047: stelem.ref
84 IL_0048: dup
85 IL_0049: ldc.i4.2
86 IL_004a: ldstr bytearray (
87 20 00 e9 00 20 00 6d 00 61 00 69 00 6f 00 72 00 // ... .m.a.i.o.r
.
88 20 00 71 00 75 00 65 00 20 00 6f 00 20 00 73 00 // .q.u.e. .o. .s
.
89 65 00 67 00 75 00 6e 00 64 00 6f 00 20 00 01 ) // e.g.u.n.d.o. ..
90
91 IL_004f: stelem.ref
92 IL_0050: dup
93 IL_0051: ldc.i4.3
94 IL_0052: ldloc.1
95 IL_0053: box [mscorlib]System.Int32
96 IL_0058: stelem.ref
97 IL_0059: call string string::Concat(object[])
98 IL_005e: call void class [mscorlib]System.Console::Write(string)
99 IL_0063: br IL_009a
1.3
100
101 IL_0068: ldc.i4.4
102 IL_0069: newarr [mscorlib]System.Object
103 IL_006e: dup
104 IL_006f: ldc.i4.0
105 IL_0070: ldstr bytearray (
106 4f 00 20 00 73 00 65 00 67 00 75 00 6e 00 64 00 // O. .s.e.g.u.n.d
.
107 6f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // o. .n...m.e.r.o
.
108 20 00 01 ) // ..
109
110 IL_0075: stelem.ref
111 IL_0076: dup
112 IL_0077: ldc.i4.1
113 IL_0078: ldloc.1
114 IL_0079: box [mscorlib]System.Int32
115 IL_007e: stelem.ref
116 IL_007f: dup
117 IL_0080: ldc.i4.2
118 IL_0081: ldstr bytearray (
119 20 00 e9 00 20 00 6d 00 61 00 69 00 6f 00 72 00 // ... .m.a.i.o.r
.
120 20 00 71 00 75 00 65 00 20 00 6f 00 20 00 70 00 // .q.u.e. .o. .p
.
121 72 00 69 00 6d 00 65 00 69 00 72 00 6f 00 01 ) // r.i.m.e.i.r.o..
122
123 IL_0086: stelem.ref
124 IL_0087: dup
125 IL_0088: ldc.i4.3
126 IL_0089: ldloc.1
127 IL_008a: box [mscorlib]System.Int32
128 IL_008f: stelem.ref
129 IL_0090: call string string::Concat(object[])
130 IL_0095: call void class [mscorlib]System.Console::Write(string)
131 IL_009a: ret
132 } // end of method micro02::Main
133
134 } // end of class micro02
15. Micro03
Listagem 1.43: micro03.pas
1 PROGRAM MICRO03;
2 VAR
3 numero: integer;
4
5 BEGIN
6 write('Digite um nmero: ');
7 readln(numero);
8 if ( numero >= 100 ) then
9 begin
10 if ( numero <= 200 ) then
11 begin
12 writeln('O nmero est no intervalo entre 100 e 200');
13 end;
14 else
15 begin
1.3
17
18 .class private auto ansi beforefieldinit micro03
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
1.3
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method micro03::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 81 (0x51)
41 .maxstack 2
42 .locals init (
43 int32 V_0)
44 IL_0000: ldstr bytearray (
45 44 00 69 00 67 00 69 00 74 00 65 00 20 00 75 00 // D.i.g.i.t.e. .u
.
46 6d 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // m. .n...m.e.r.o
.
47 3a 00 20 00 01 ) // :. ..
48
49 IL_0005: call void class [mscorlib]System.Console::Write(string)
50 IL_000a: call string class [mscorlib]System.Console::ReadLine()
51 IL_000f: call int32 class [mscorlib]System.Convert::ToInt32(string
)
52 IL_0014: stloc.0
53 IL_0015: ldloc.0
54 IL_0016: ldc.i4.s 0x64
55 IL_0018: blt IL_0046
56
57 IL_001d: ldloc.0
58 IL_001e: ldc.i4 200
59 IL_0023: bgt IL_0037
60
61 IL_0028: ldstr bytearray (
62 4f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // O. .n...m.e.r.o
.
63 20 00 6e 00 6f 00 20 00 69 00 6e 00 74 00 65 00 // .n.o. .i.n.t.e
.
64 72 00 76 00 61 00 6c 00 6f 00 72 00 20 00 65 00 // r.v.a.l.o.r. .e
.
65 6e 00 74 00 72 00 65 00 20 00 31 00 30 00 30 00 // n.t.r.e.
.1.0.0.
66 20 00 65 00 20 00 32 00 30 00 30 00 01 ) // .e. .2.0.0..
67
68 IL_002d: call void class [mscorlib]System.Console::WriteLine(
string)
69 IL_0032: br IL_0041
70
71 IL_0037: ldstr bytearray (
72 4f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // O. .n...m.e.r.o
.
73 20 00 6e 00 e3 00 6f 00 20 00 65 00 73 00 74 00 // .n...o. .e.s.t
.
1.3
74 e1 00 20 00 6e 00 6f 00 20 00 69 00 6e 00 74 00 // .. .n.o. .i.n.t
.
75 65 00 72 00 76 00 61 00 6c 00 6f 00 20 00 65 00 // e.r.v.a.l.o. .e
.
76 6e 00 74 00 72 00 65 00 20 00 31 00 30 00 30 00 // n.t.r.e.
.1.0.0.
77 20 00 65 00 20 00 32 00 30 00 30 00 01 ) // .e. .2.0.0..
78
79 IL_003c: call void class [mscorlib]System.Console::WriteLine(
string)
80 IL_0041: br IL_0050
81
82 IL_0046: ldstr bytearray (
83 4f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // O. .n...m.e.r.o
.
84 20 00 6e 00 e3 00 6f 00 20 00 65 00 73 00 74 00 // .n...o. .e.s.t
.
85 e1 00 20 00 6e 00 6f 00 20 00 69 00 6e 00 74 00 // .. .n.o. .i.n.t
.
86 65 00 72 00 76 00 61 00 6c 00 6f 00 20 00 65 00 // e.r.v.a.l.o. .e
.
87 6e 00 74 00 72 00 65 00 20 00 31 00 30 00 30 00 // n.t.r.e.
.1.0.0.
88 20 00 65 00 20 00 32 00 30 00 30 00 01 ) // .e. .2.0.0..
89
90 IL_004b: call void class [mscorlib]System.Console::WriteLine(
string)
91 IL_0050: ret
92 } // end of method micro03::Main
93
94 } // end of class micro03
16. Micro04
Listagem 1.46: micro04.pas
1 PROGRAM MICRO04;
2 VAR
3 x, num, intervalo : integer;
4
5 BEGIN
6 intervalo := 0;
7 for x := 1 to 5 do
8 begin
9 write('Digite um nmero: ');
10 readln(num);
11 if ( num >= 10 ) then
12 begin
13 if ( num <= 150 ) then
14 begin
15 intervalo := intervalo + 1;
16 end;
17 end;
18 end;
19 writeln('Ao total, foram digitados ', intervalo, ' nmeros no
intervalor 10 e 150');
20 END.
1.3
3 class micro04 {
4 public static void Main(){
5 int x, num, intervalo;
6
7 intervalo = 0;
8
69 IL_0031: ldloc.2
70 IL_0032: ldc.i4.1
71 IL_0033: add
72 IL_0034: stloc.2
73 IL_0035: ldloc.0
74 IL_0036: ldc.i4.1
75 IL_0037: add
76 IL_0038: stloc.0
77 IL_0039: ldloc.0
78 IL_003a: ldc.i4.5
79 IL_003b: ble IL_0009
80
.
87 72 00 76 00 61 00 6c 00 6f 00 20 00 65 00 6e 00 // r.v.a.l.o. .e.n
.
88 74 00 72 00 65 00 20 00 31 00 30 00 20 00 65 00 // t.r.e. .1.0. .e
.
89 20 00 31 00 35 00 30 00 01 ) // .1.5.0..
90
91 IL_0050: call string string::Concat(object, object, object)
92 IL_0055: call void class [mscorlib]System.Console::WriteLine(
string)
93 IL_005a: ret
94 } // end of method micro04::Main
95
96 } // end of class micro04
17. Micro05
Listagem 1.49: micro05.pas
1 PROGRAM MICRO05;
2 VAR
3
10 m = 0;
11
12 for ( x = 1; x <= 5; x++){
13 Console.Write("Digite o nome: ");
14 nome = Console.ReadLine();
15 Console.Write("H - Homem ou M - Mulher: ");
16 sexo = Convert.ToChar(Console.Read());
17 switch (sexo){
18 case 'H': h = h + 1; break;
19 case 'M': m = m + 1; break;
20 default: Console.WriteLine("Sexo s pode ser H ou M!"); break
;
21 }
22 }
23
24 Console.WriteLine("Foram inseridos " + h + " Homens");
25 Console.WriteLine("Foram inseridos " + m + " Mulheres");
26 }
27 }
69 IL_0039: ldloc.1
70 IL_003a: ldc.i4.s 0x4d
71 IL_003c: beq IL_004f
72
73 IL_0041: br IL_005a
74
75 IL_0046: ldloc.3
76 IL_0047: ldc.i4.1
77 IL_0048: add
78 IL_0049: stloc.3
79 IL_004a: br IL_0069
80
81 IL_004f: ldloc.s 4
82 IL_0051: ldc.i4.1
83 IL_0052: add
84 IL_0053: stloc.s 4
85 IL_0055: br IL_0069
86
91 01 ) // .
92
93 IL_005f: call void class [mscorlib]System.Console::WriteLine(
string)
94 IL_0064: br IL_0069
95
96 IL_0069: ldloc.2
97 IL_006a: ldc.i4.1
98 IL_006b: add
99 IL_006c: stloc.2
100 IL_006d: ldloc.2
101 IL_006e: ldc.i4.5
102 IL_006f: ble IL_000c
103
104 IL_0074: ldstr "Foram inseridos "
105 IL_0079: ldloc.3
106 IL_007a: box [mscorlib]System.Int32
107 IL_007f: ldstr " Homens"
108 IL_0084: call string string::Concat(object, object, object)
109 IL_0089: call void class [mscorlib]System.Console::WriteLine(
string)
110 IL_008e: ldstr "Foram inseridos "
111 IL_0093: ldloc.s 4
112 IL_0095: box [mscorlib]System.Int32
113 IL_009a: ldstr " Mulheres"
114 IL_009f: call string string::Concat(object, object, object)
115 IL_00a4: call void class [mscorlib]System.Console::WriteLine(
string)
116 IL_00a9: ret
117 } // end of method micro05::Main
118
119 } // end of class micro05
18. Micro06
Listagem 1.52: micro06.pas
1 PROGRAM MICRO06;
2 VAR
3 numero : integer;
4
5 BEGIN
6 write('Digite um nmero de 1 a 5: ');
7 readln(numero);
8 case numero of
9 1 : writeln('Um');
10 2 : writeln('Dois');
11 3 : writeln('Trs');
12 4 : writeln('Quatro');
13 5 : writeln('Cinco');
14 otherwise writeln('Nmero Invlido!!!');
15 end;
16 END.
39 .entrypoint
40 // Code size 145 (0x91)
41 .maxstack 2
42 .locals init (
43 int32 V_0)
44 IL_0000: ldstr bytearray (
45 44 00 69 00 67 00 69 00 74 00 65 00 20 00 75 00 // D.i.g.i.t.e. .u
.
46 6d 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // m. .n...m.e.r.o
.
47 20 00 64 00 65 00 20 00 31 00 20 00 61 00 20 00 // .d.e. .1. .a.
.
48 35 00 3a 00 20 00 01 ) // 5.:. ..
49
50 IL_0005: call void class [mscorlib]System.Console::Write(string)
51 IL_000a: call string class [mscorlib]System.Console::ReadLine()
52 IL_000f: call int32 class [mscorlib]System.Convert::ToInt32(string
)
53 IL_0014: stloc.0
54 IL_0015: ldloc.0
55 IL_0016: ldc.i4.1
56 IL_0017: sub
57 IL_0018: switch (
58 IL_0036,
59 IL_0045,
60 IL_0054,
61 IL_0063,
62 IL_0072)
63 IL_0031: br IL_0081
64
65 IL_0036: ldstr "Um"
66 IL_003b: call void class [mscorlib]System.Console::WriteLine(
string)
67 IL_0040: br IL_0090
68
69 IL_0045: ldstr "Dois"
70 IL_004a: call void class [mscorlib]System.Console::WriteLine(
string)
71 IL_004f: br IL_0090
72
.
89 6e 00 76 00 e1 00 6c 00 69 00 64 00 6f 00 21 00 // n.v...l.i.d.o
.!.
90 21 00 21 00 01 ) // !.!..
91
92 IL_0086: call void class [mscorlib]System.Console::WriteLine(
string)
93 IL_008b: br IL_0090
94
95 IL_0090: ret
96 } // end of method micro06::Main
97
98 } // end of class micro06
19. Micro07
Listagem 1.55: micro07.pas
1 PROGRAM MICRO07;
2 VAR
3 programa, numero : integer;
4 opc : char;
5
6 BEGIN
7 programa := 1;
8 while ( programa = 1 ) do
9 begin
10 write('Digite um nmero: ');
11 readln(numero);
12 if ( numero > 0 ) then
13 begin
14 writeln('Positivo');
15 end;
16 else
17 begin
18 if ( numero = 0 ) then
19 begin
20 writeln('O nmero igual a 0');
21 end;
22 if ( numero < 0 ) then
23 begin
24 writeln('Negativo');
25 end;
26 end;
27 write('Deseja finalizar? (S/N) ');
28 readln(opc);
29 if ( opc = 'S' ) then
30 begin
31 programa := 0;
32 end;
33 end;
34 END.
3 class micro07 {
4 public static void Main(){
1.3
17
18 .class private auto ansi beforefieldinit micro07
19 extends [mscorlib]System.Object
20 {
21
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
1.3
31 IL_0006: ret
32 } // end of method micro07::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 122 (0x7a)
41 .maxstack 2
42 .locals init (
43 int32 V_0,
44 int32 V_1,
45 char V_2)
46 IL_0000: ldc.i4.1
47 IL_0001: stloc.0
48 IL_0002: br IL_0072
49
50 IL_0007: ldstr bytearray (
51 44 00 69 00 67 00 69 00 74 00 65 00 20 00 75 00 // D.i.g.i.t.e. .u
.
52 6d 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // m. .n...m.e.r.o
.
53 3a 00 20 00 01 ) // :. ..
54
55 IL_000c: call void class [mscorlib]System.Console::WriteLine(
string)
56 IL_0011: call string class [mscorlib]System.Console::ReadLine()
57 IL_0016: call int32 class [mscorlib]System.Convert::ToInt32(string
)
58 IL_001b: stloc.1
59 IL_001c: ldloc.1
60 IL_001d: ldc.i4.0
61 IL_001e: ble IL_0032
62
63 IL_0023: ldstr "Positivo"
64 IL_0028: call void class [mscorlib]System.Console::WriteLine(
string)
65 IL_002d: br IL_0053
66
67 IL_0032: ldloc.1
68 IL_0033: brtrue IL_0042
69
70 IL_0038: ldstr bytearray (
71 4f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // O. .n...m.e.r.o
.
72 20 00 e9 00 20 00 69 00 67 00 75 00 61 00 6c 00 // ... .i.g.u.a.l
.
73 20 00 61 00 20 00 30 00 01 ) // .a. .0..
74
75 IL_003d: call void class [mscorlib]System.Console::WriteLine(
string)
76 IL_0042: ldloc.1
77 IL_0043: ldc.i4.0
78 IL_0044: bge IL_0053
79
80 IL_0049: ldstr "Negativo"
81 IL_004e: call void class [mscorlib]System.Console::WriteLine(
1.3
string)
82 IL_0053: ldstr "Desja finalizar? (S/N) "
83 IL_0058: call void class [mscorlib]System.Console::Write(string)
84 IL_005d: call string class [mscorlib]System.Console::ReadLine()
85 IL_0062: call char class [mscorlib]System.Convert::ToChar(string)
86 IL_0067: stloc.2
87 IL_0068: ldloc.2
88 IL_0069: ldc.i4.s 0x53
89 IL_006b: bne.un IL_0072
90
91 IL_0070: ldc.i4.0
92 IL_0071: stloc.0
93 IL_0072: ldloc.0
94 IL_0073: ldc.i4.1
95 IL_0074: beq IL_0007
96
97 IL_0079: ret
98 } // end of method micro07::Main
99
100 } // end of class micro07
20. Micro08
Listagem 1.58: micro08.pas
1 PROGRAM MICRO08;
2 VAR
3 numero : integer;
4
5 BEGIN
6 numero := 1;
7 while ( numero <> 0 ) do
8 begin
9 write('Digite um nmero: ');
10 readln(numero);
11
12 if ( numero > 10 ) then
13 begin
14 writeln('O nmero ', numero, ' maior que 10');
15 end;
16 else
17 begin
18 writeln('O nmero ', numero, ' menor que 10');
19 end;
20 end;
21 END.
3 class micro08 {
4 public static void Main(){
5 int numero;
6
7 numero = 1;
8 while ( numero != 0 ) {
9 Console.Write("Digite um nmero: ");
10 numero = Convert.ToInt32(Console.ReadLine());
1.3
11 if ( numero > 10 )
12 Console.WriteLine("O nmero " + numero + " maior que 10");
13 else
14 Console.WriteLine("O nmero " + numero + " menor que 10");
15 }
16 }
17 }
22 // method line 1
23 .method public hidebysig specialname rtspecialname
24 instance default void '.ctor' () cil managed
25 {
26 // Method begins at RVA 0x2050
27 // Code size 7 (0x7)
28 .maxstack 8
29 IL_0000: ldarg.0
30 IL_0001: call instance void object::'.ctor'()
31 IL_0006: ret
32 } // end of method micro08::.ctor
33
34 // method line 2
35 .method public static hidebysig
36 default void Main () cil managed
37 {
38 // Method begins at RVA 0x2058
39 .entrypoint
40 // Code size 100 (0x64)
41 .maxstack 3
42 .locals init (
43 int32 V_0)
44 IL_0000: ldc.i4.1
45 IL_0001: stloc.0
46 IL_0002: br IL_005d
1.3
47
48 IL_0007: ldstr bytearray (
49 44 00 69 00 67 00 69 00 74 00 65 00 20 00 75 00 // D.i.g.i.t.e. .u
.
50 6d 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // m. .n...m.e.r.o
.
51 3a 00 20 00 01 ) // :. ..
52
53 IL_000c: call void class [mscorlib]System.Console::Write(string)
54 IL_0011: call string class [mscorlib]System.Console::ReadLine()
55 IL_0016: call int32 class [mscorlib]System.Convert::ToInt32(string
)
56 IL_001b: stloc.0
57 IL_001c: ldloc.0
58 IL_001d: ldc.i4.s 0x0a
59 IL_001f: ble IL_0043
60
61 IL_0024: ldstr bytearray (
62 4f 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // O. .n...m.e.r.o
.
63 20 00 01 ) // ..
64
65 IL_0029: ldloc.0
66 IL_002a: box [mscorlib]System.Int32
67 IL_002f: ldstr bytearray (
68 20 00 e9 00 20 00 6d 00 61 00 69 00 6f 00 72 00 // ... .m.a.i.o.r
.
69 20 00 71 00 75 00 65 00 20 00 31 00 30 00 01 ) // .q.u.e. .1.0..
70
71 IL_0034: call string string::Concat(object, object, object)
72 IL_0039: call void class [mscorlib]System.Console::WriteLine(
string)
73 IL_003e: br IL_005d
74
90 IL_0063: ret
91 } // end of method micro08::Main
92
93 } // end of class micro08
21. Micro09
1.3
52 IL_001e: stloc.0
53 IL_001f: ldstr "Digite a venda: "
54 IL_0024: call void class [mscorlib]System.Console::Write(string)
55 IL_0029: call string class [mscorlib]System.Console::ReadLine()
56 IL_002e: call float64 class [mscorlib]System.Convert::ToDouble(
string)
57 IL_0033: stloc.1
58 IL_0034: ldloc.1
59 IL_0035: ldc.r8 500.
60 IL_003e: blt IL_0052
61
62 IL_0043: ldloc.0
63 IL_0044: ldc.r8 30.
64 IL_004d: bge.un IL_0065
65
66 IL_0052: ldloc.0
67 IL_0053: ldc.r8 0.
68 IL_005c: ldloc.0
69 IL_005d: mul
70 IL_005e: add
71 IL_005f: stloc.2
72 IL_0060: br IL_00e0
73
74 IL_0065: ldloc.1
75 IL_0066: ldc.r8 500.
76 IL_006f: blt.un IL_0083
77
78 IL_0074: ldloc.1
79 IL_0075: ldc.r8 1200.
80 IL_007e: blt IL_00a1
81
82 IL_0083: ldloc.0
83 IL_0084: ldc.r8 30.
84 IL_008d: blt.un IL_00b4
85
86 IL_0092: ldloc.0
87 IL_0093: ldc.r8 80.
88 IL_009c: bge.un IL_00b4
89
90 IL_00a1: ldloc.0
91 IL_00a2: ldc.r8 0.
92 IL_00ab: ldloc.0
93 IL_00ac: mul
94 IL_00ad: add
95 IL_00ae: stloc.2
96 IL_00af: br IL_00e0
97
98 IL_00b4: ldloc.1
99 IL_00b5: ldc.r8 1200.
100 IL_00be: bge IL_00d2
101
102 IL_00c3: ldloc.0
103 IL_00c4: ldc.r8 80.
104 IL_00cd: blt.un IL_00e0
105
106 IL_00d2: ldloc.0
107 IL_00d3: ldc.r8 0.
108 IL_00dc: ldloc.0
109 IL_00dd: mul
1.3
22. Micro10
Listagem 1.64: micro10.pas
1 PROGRAM MICRO10;
2 VAR
3 numero : integer;
4 fat : integer;
5
6 FUNCTION fatorial( numero : integer) : integer;
7 begin
8 if ( numero <= 0) then
9 begin
10 fatorial := 1;
11 end;
12
13 else
14 begin
15 fatorial := numero * fatorial(numero-(1));
16 end;
17 end;
18
19 BEGIN
20 write('Digite um nmero: ');
21 readln(numero);
22 fat := fatorial(numero);
23 write('O fatorial de ');
24 write(numero);
25 write(' ');
26 writeln(fat);
27 END.
44
45 IL_0007: ldc.i4.1
46 IL_0008: ret
47 IL_0009: ldarg.0
48 IL_000a: ldarg.0
49 IL_000b: ldc.i4.1
50 IL_000c: sub
51 IL_000d: call int32 class micro10::fatorial(int32)
52 IL_0012: mul
53 IL_0013: ret
54 } // end of method micro10::fatorial
55
56 // method line 3
57 .method public static hidebysig
58 default void Main () cil managed
59 {
60 // Method begins at RVA 0x2070
61 .entrypoint
62 // Code size 61 (0x3d)
63 .maxstack 1
64 .locals init (
65 int32 V_0,
66 int32 V_1)
67 IL_0000: ldstr bytearray (
68 44 00 69 00 67 00 69 00 74 00 65 00 20 00 75 00 // D.i.g.i.t.e. .u
.
69 6d 00 20 00 6e 00 fa 00 6d 00 65 00 72 00 6f 00 // m. .n...m.e.r.o
.
70 3a 00 20 00 01 ) // :. ..
71
72 IL_0005: call void class [mscorlib]System.Console::Write(string)
73 IL_000a: call string class [mscorlib]System.Console::ReadLine()
74 IL_000f: call int32 class [mscorlib]System.Convert::ToInt32(string
)
75 IL_0014: stloc.0
76 IL_0015: ldloc.0
77 IL_0016: call int32 class micro10::fatorial(int32)
78 IL_001b: stloc.1
79 IL_001c: ldstr "O fatorial de "
80 IL_0021: call void class [mscorlib]System.Console::Write(string)
81 IL_0026: ldloc.0
82 IL_0027: call void class [mscorlib]System.Console::Write(int32)
83 IL_002c: ldstr bytearray (
84 20 00 e9 00 20 00 01 ) // ... ..
85
86 IL_0031: call void class [mscorlib]System.Console::Write(string)
87 IL_0036: ldloc.1
88 IL_0037: call void class [mscorlib]System.Console::WriteLine(int32
)
89 IL_003c: ret
90 } // end of method micro10::Main
91
23. Micro11
Listagem 1.67: micro11.pas
1 PROGRAM MICRO11;
1.3
2 VAR
3 numero : integer;
4 x : integer;
5
12 return res;
13 }
14
15 public static void Main(){
16 int numero, x;
17
18 Console.Write("Digite um nmero: ");
19 numero = Convert.ToInt32(Console.ReadLine());
20 x = verifica(numero);
21 if ( x == 1 )
22 Console.WriteLine("Numero positivo");
23 else if ( x == 0)
24 Console.WriteLine("Zero");
25 else Console.WriteLine("Numero negativo");
26 }
27 }
string)
94 IL_002d: br IL_0051
95
96 IL_0032: ldloc.1
97 IL_0033: brtrue IL_0047
98
99 IL_0038: ldstr "Zero"
100 IL_003d: call void class [mscorlib]System.Console::WriteLine(
string)
101 IL_0042: br IL_0051
102
103 IL_0047: ldstr "Numero negativo"
104 IL_004c: call void class [mscorlib]System.Console::WriteLine(
string)
105 IL_0051: ret
106 } // end of method micro11::Main
107
108 } // end of class micro11
Captulo 2
Analisador Lxico
Anlise lxica (scanning) o primeiro estgio de um processo em trs partes que o com-
pilador utiliza para entender o programa de entrada. O scanner, ou analisador lxico, l um
fluxo de caracteres e produz um fluxo de palavras. Ele agrega caracteres para formar pala-
vras e aplica um conjunto de regras para determinar se cada uma delas uma palavra vlida
na linguagem-fonte. Se a palavra vlida, o scanner atribui-lhe uma categoria sinttica, ou
classe gramatical. Eis o analisador lxico em Ocaml da linguagem Pascal:
Listagem 2.1: lexico.mll
1 {
2 open Lexing
3 open Printf
4 open Sintatico
5
6 exception Erro of string
7
8 let incr_num_linha lexbuf =
9 let pos = lexbuf.lex_curr_p in
10 lexbuf.lex_curr_p <-
11 { pos with pos_lnum = pos.pos_lnum + 1;
12 pos_bol = pos.pos_cnum
13 }
14
15 let pos_atual lexbuf = lexbuf.lex_start_p
16
17 }
18
25
26 let identificador = letra ( letra | digito | '_')*
27
28 let brancos = [' ' '\t']+
29 let novalinha = '\r' | '\n' | "\r\n"
30
60
2.0
Analisador Sinttico
63
3.0
30
31 %token<Lexing.position> PROGRAM
32 %token<Lexing.position> BEGIN
33 %token<Lexing.position> END
34
35 %token<Lexing.position> IF THEN ELSE
36 %token<Lexing.position> CASE
37 %token<Lexing.position> OTHERWISE
38 %token<Lexing.position> READLN
39 %token<Lexing.position> WRITE WRITELN
40 %token<Lexing.position> ATRIBUICAO
41
42 %token<Lexing.position> OR
43 %token<Lexing.position> AND
44 %token<Lexing.position> NOT
45 %token<Lexing.position> IGUAL
46 %token<Lexing.position> DIFERENTE
47 %token<Lexing.position> MAIOR
48 %token<Lexing.position> MAIOR_IGUAL
49 %token<Lexing.position> MENOR
50 %token<Lexing.position> MENOR_IGUAL
51
52 %token<Lexing.position> MAIS
53 %token<Lexing.position> MENOS
54 %token<Lexing.position> MULTIPLICACAO
55 %token<Lexing.position> DIVISAO
56 %token<Lexing.position> MOD
57
58
59 %token<Lexing.position> ABRE_PARENTESE FECHA_PARENTESE
60 %token EOF
61
62 %right NOT
63 %left OR
64 %left AND
65 %left IGUAL DIFERENTE MAIOR MENOR MAIOR_IGUAL MENOR_IGUAL
66 %left MULTIPLICACAO DIVISAO MOD
67 %left MAIS MENOS
68
69 %start <Sast.expressao Ast.programa> programa
70
71 %%
72
73 programa: PROGRAM identificador=IDENTIFICADOR PONTO_VIRGULA
74 VAR
75 ds = declaracao*
76 fs = funcao*
77 BEGIN
78 cs = comando*
79 END PONTO
80 EOF { Programa (identificador, List.flatten ds, fs, cs) }
81
88 }
89
90 funcao: FUNCTION id=IDENTIFICADOR ABRE_PARENTESE parametros=separated_list
(VIRGULA, argumento) FECHA_PARENTESE DOIS_PONTOS r=tipo PONTO_VIRGULA
91 VAR
92 ds = declaracao*
93 BEGIN
94 cs = comando*
95 END PONTO_VIRGULA {
96 DecFun {
97 fn_nome = id;
98 fn_tiporet = r;
99 fn_formais = parametros;
100 fn_locais = List.flatten ds;
101 fn_corpo = cs
102 }
103 }
104
105
106 tipo: INTEGER { TipoInteger }
107 | REAL { TipoReal }
108 | STRING { TipoString }
109 | CHAR { TipoString }
110 | BOOLEAN { TipoBool }
111
112
113 comando: c=comando_atribuicao { c }
114 | c=comando_if { c }
115 | c=comando_readln { c }
116 | c=comando_write { c }
117 | c=comando_for { c }
118 | c=comando_while { c }
119 | c=comando_case { c }
120 | c=comando_chamada_funcao { c }
121
122
123 comando_atribuicao: esq=expressao ATRIBUICAO dir=expressao PONTO_VIRGULA {
124 CmdAtrib (esq,dir)
125 }
126
127
128
129
130 comando_if: IF ABRE_PARENTESE teste=expressao FECHA_PARENTESE THEN
131 BEGIN
132 entao=comando*
133 END PONTO_VIRGULA
134 senao=option ( ELSE
135 BEGIN
136 cs = comando*
137 END PONTO_VIRGULA {cs} ) {
138 CmdIf (teste, entao, senao)
139 }
140
141 comando_readln: READLN ABRE_PARENTESE xs=separated_nonempty_list(VIRGULA,
expressao) FECHA_PARENTESE PONTO_VIRGULA {
142 CmdReadln xs
143 }
144
3.0
145 funcaoSaida:
146 | c=WRITE { c }
147 | c=WRITELN { c }
148
58 and operacao =
59 | Mais
60 | Menos
61 | Multiplicacao
62 | Divisao
63 | Mod
64 | Menor
65 | MenorIgual
66 | Maior
67 | MaiorIgual
68 | Igual
69 | Diferente
70 | Or
71 | And
72 | Not
Analisador Semntico
69
4.0
20 let classifica op =
21 let open A in
22 match op with
23 | Not
24 | Or
25 | And -> Logico
26 | Menor
27 | MenorIgual
28 | Maior
29 | MaiorIgual
30 | Igual
31 | Diferente -> Relacional
32 | Mod
33 | Mais
34 | Menos
35 | Multiplicacao
36 | Divisao -> Aritmetico
37
*)
73 (* encontar tenta novamente no escopo que engloba o atual.
Prossegue-se *)
74 (* assim at encontrar a definio em algum escopo englobante ou at
*)
75 (* encontrar o escopo global. Se em algum lugar for encontrado,
*)
76 (* devolve-se a definio. Em caso contrrio, devolve uma exceo
*)
77 let id = fst nome in
78 (try (match (Amb.busca amb id) with
79 | Amb.EntVar tipo -> (T.ExpressaoVariavel (A.VarSimples
nome, tipo), tipo)
80 | Amb.EntFun _ ->
81 let msg = "nome de funcao usado como nome de variavel: "
^ id in
82 failwith (msg_erro nome msg)
83 )
84 with Not_found ->
85 let msg = "A variavel " ^ id ^ " nao foi declarada" in
86 failwith (msg_erro nome msg)
87 )
88 (* | _ -> failwith "infere_exp: no implementado" *)
89 )
90 | S.ExpressaoOperador (op, esq, dir) ->
91 let (esq, tesq) = infere_exp amb esq
92 and (dir, tdir) = infere_exp amb dir in
93
94 let verifica_aritmetico () =
95 (match tesq with
96 A.TipoInteger ->
97 let _ = mesmo_tipo (snd op)
98 "O operando esquerdo eh do tipo %s mas o direito eh
do tipo %s"
99 tesq tdir
100 in tesq (* O tipo da expresso aritmtica como um todo *)
101
102 | A.TipoReal ->
103 let _ = mesmo_tipo (snd op)
104 "O operando esquerdo eh do tipo %s mas o direito eh
do tipo %s"
105 tesq tdir
106 in tesq (* O tipo da expresso aritmtica como um todo *)
107
108 | t -> let msg = "um operador aritmetico nao pode ser usado com o
tipo " ^
109 (nome_tipo t)
110 in failwith (msg_erro_pos (snd op) msg)
111 )
112
113 and verifica_relacional () =
114 (match tesq with
115 A.TipoInteger
116 | A.TipoReal
117 | A.TipoString ->
118 let _ = mesmo_tipo (snd op)
119 "O operando esquerdo eh do tipo %s mas o direito eh do
tipo %s"
120 tesq tdir
4.0
149 | t -> let msg = "um operador relacional nao pode ser usado com o
tipo " ^
150 (nome_tipo t)
151 in failwith (msg_erro_pos (snd op) msg)
152 )
153 *)
154 in
155 let op = fst op in
156 let tinf = (match (classifica op) with
157 Aritmetico -> verifica_aritmetico ()
158 | Relacional -> verifica_relacional ()
159 | Logico -> verifica_logico ()
160 (* | Cadeia -> verifica_cadeia () *)
161 )
162 in
163 (T.ExpressaoOperador ((op,tinf), (esq, tesq), (dir, tdir)), tinf)
164
165 | S.ExpressaoChamadaFuncao (nome, args) ->
166 let rec verifica_parametros ags ps fs =
167 match (ags, ps, fs) with
168 (a::ags), (p::ps), (f::fs) ->
169 let _ = mesmo_tipo (posicao a)
170 "O parametro eh do tipo %s mas deveria ser do tipo %s
" p f
171 in verifica_parametros ags ps fs
4.0
booleano *)
222 let _ = mesmo_tipo (posicao teste)
223 "O teste do if deveria ser do tipo %s e nao %s"
224 TipoBool tinf in
225 (* Verifica a validade de cada comando do bloco 'ento' *)
226 let entao1 = List.map (verifica_cmd amb tiporet) entao in
227 (* Verifica a validade de cada comando do bloco 'seno', se houver *)
228 let senao1 =
229 match senao with
230 None -> None
231 | Some bloco -> Some (List.map (verifica_cmd amb tiporet) bloco)
232 in
233 CmdIf (teste1, entao1, senao1)
234
235
236 (*
237 | ExpressaoVariavel v -> (match v with
238 | A.VarSimples (_,pos) -> pos
239 )
240 *)
241
242 | CmdAtrib (elem, exp) -> let open Amb in
243 let (exp2, tdir) = infere_exp amb exp in
244 (match elem with
245 S.ExpressaoVariavel v ->
246 ( match v with
247 | A.VarSimples(ch,_) ->
248 ( try
249 (match (Amb.busca amb ch ) with
250 Amb.EntVar tipo -> let (elem_tip, telem) =
infere_exp amb elem
251 and (exp_tip, ttip) = infere_exp amb exp
in
252 let _ = mesmo_tipo (posicao elem) "
Atribuicao com tipos diferentes: %s = %
s"
253 telem ttip
254 in CmdAtrib (elem_tip, exp_tip)
255
256 | Amb.EntFun { tipo_fn; _} -> ( match tipo_fn
with
257 TipoVoid -> let _ = mesmo_tipo (
posicao elem) "Funcao do tipo %
s nao pode receber o valor do
tipo %s"
258 TipoVoid tdir in
259 CmdReturn (None)
260 | tipo -> let _ = mesmo_tipo (
posicao elem) "Funcao do tipo %s
nao pode receber o valor do tipo
%s"
261 tipo tdir in
262 CmdReturn (Some exp2)
263
264 )
265 )
266 with Not_found -> failwith ("A varivel " ^ ch ^ " no
foi declarada")
267 )
4.0
268 )
269 | _ -> failwith ""
270 )
271
272
273 | CmdCase (teste, corpo, senao) ->
274 (* Infere o tipo da variavel a ser tomada no case *)
275 let (teste1,tinf) = infere_exp amb teste in
276
277 (* Verifica a validade de cada comando do bloco 'corpo' *)
278 let corpo1 = List.map (verifica_cmd amb tiporet) corpo in
279
280 (* Verifica a validade de cada comando do bloco 'seno', se houver *)
281 let senao1 =
282 match senao with
283 None -> None
284 | Some bloco -> Some (List.map (verifica_cmd amb tiporet) bloco)
285 in
286 CmdCase(teste1, corpo1, senao1)
287
288 | CmdOpCase (expressao, comando) ->
289 (* Verifica a validade da expressao *)
290 let (expressao1,tinf) = infere_exp amb expressao in
291
292 (* Verifica a validade de cada comando do bloco 'comandos' *)
293 let comando1 = verifica_cmd amb tiporet comando in
294 CmdOpCase (expressao1, comando1)
295
384
385 (* Lista de cabealhos das funes pr definidas *)
386 let fn_predefs = let open A in [
387 ("readln", [("x", TipoInteger); ("y", TipoInteger)], TipoVoid);
388 ("write", [("x", TipoInteger); ("y", TipoInteger)], TipoVoid);
389 ("writeln", [("x", TipoInteger); ("y", TipoInteger)], TipoVoid)
390 ]
391
392 (* insere as funes pr definidas no ambiente global *)
393 let declara_predefinidas amb =
394 List.iter (fun (n,ps,tr) -> Amb.insere_fun amb n ps tr) fn_predefs
395
Interprete
78
5.0
246 )
247 )
248
249
250 (*
251 | CmdAtrib (elem, exp) ->
252 (* Interpreta o lado direito da atribuio *)
253 let exp = interpreta_exp amb exp
254 (* Faz o mesmo para o lado esquerdo *)
255 and (elem1,tipo) = obtem_nome_tipo_var elem in
256 Amb.atualiza_var amb elem1 tipo (Some exp)
257 *)
258
259 | CmdAtrib (elem, exp) ->
260 (* Interpreta o lado direito da atribuio *)
261 let exp = interpreta_exp amb exp
262 (* Faz o mesmo para o lado esquerdo *)
263 and (elem1,tipo) = obtem_nome_tipo_var elem in
264 Amb.atualiza_var amb elem1 tipo (Some exp)
265
266 | CmdChamFunc exp -> ignore ( interpreta_exp amb exp )
267