Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more aout F# at http://fsharp.org
- // See the 'F# Tutorial' project for more help.
- type instruction =
- | ADD
- | SUB
- | MUL
- | SQR
- | PUSH of float
- type stack = float list
- let wypisz_instr = function
- | (PUSH x) -> "PUSH" + System.Convert.ToString( x )
- | (SQR)-> " SQR "
- | (ADD) -> " ADD "
- | (SUB) -> " SUB "
- | (MUL) -> " MUL "
- let rec wypisz_inst_wszytkie = function
- | ([], x::xs) -> printf "%A\n" x
- | (i::is, xs) -> wypisz_inst_wszytkie( is, xs )
- exception BLEDNY_PROGRAM of (instruction * stack)
- let intInstr (x, y) =
- match x, y with
- | ADD, a::b::ys -> (b+a) :: ys : stack
- | SUB, a::b::ys -> (b-a) ::ys
- | MUL, a::b::ys -> (b*a)::ys
- | SQR, a::ys-> (a*a) :: ys
- | PUSH x, ys-> x::ys
- | _, _ -> raise (BLEDNY_PROGRAM(x, y))
- let intpProg(is) =
- let rec iPS = function
- | ([], x::xs) -> x
- | (i::is, xs) -> try iPS(is, intInstr(i, xs)) with
- BLEDNY_PROGRAM(a, b)->
- (
- printf "Wyjatek: Bledne wykonanie proramu \n Operand: %A, STOS: %A" a b
- wypisz_inst_wszytkie b
- b
- )
- iPS(is, [])
- [<EntryPoint>]
- let main argv =
- printfn "%A" argv
- let i1 = PUSH 3.0
- let i2 = PUSH 4.0
- let i3 =ADD
- let stos: stack=[]
- let stos1= intInstr( i1, stos)
- let stos2= intInstr( i2, stos1)
- let stos3 = intInstr( i3, stos2)
- printf "stos: %A" stos3
- //2
- let stos2:stack = [4.0; 3.0; 2.0]
- let stos2_2 = intInstr(SUB, stos2);
- let stos2_3 = intInstr(MUL, stos2_2)
- printf "stos2 = %A" stos2_3
- //3
- let stos2_4 = try intInstr(ADD, stos2_3) with
- BLEDNY_PROGRAM(a, b) ->
- (
- printf "Wyjatek: Blende wykonanie programu\n Operand: %A, STOS: %A" a b
- b
- )
- let il1 = [PUSH 3.0; PUSH 4.0; ADD; PUSH 2.0; MUL]
- let w = intpProg( il1 )
- printf "w = %A" w
- let il2 = [PUSH 3.0; PUSH 4.0; ADD; PUSH 2.0]
- let w2 = intpProg( il2 )
- printf "\n w2 = %A" w2
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement