Page 1 of 1

convert pascal code to purebasic

Posted: Fri Jan 02, 2004 12:31 pm
by patrick88
how to convert this pascal code to purebasic ???

Code: Select all

      type  opcode = (add, neg, mul, divd, remd, div2, rem2, eqli, neqi, lssi,
                leqi, gtri, geqi, dupl, swap, andb, orb,
                load, stor, hhalt, wri, wrc, wrl, rdi, rdc, rdl, eol,
                ldc, ldla, ldl, ldg, stl, stg, move, copy, addc, mulc,
                jump, jumpz, call, adjs, sets, exit);
      instr  = record Case op: opcode of
                 add, neg, mul, divd, remd, div2, rem2, eqli, neqi, lssi,
                 leqi, gtri, geqi, dupl, swap, andb, orb,
                 load, stor, hhalt, wri, wrc, wrl, rdi, rdc, rdl, eol:
                        ();
                 ldc, ldla, ldl, ldg, stl, stg, move, copy, addc, mulc,
                 jump, jumpz, call, adjs, sets, exit:
                        (a: integer)
               End;

var code: array [0..cxmax] of instr;
      m   : array [0..amax] of integer;

Procedure gen(I: instr)
Procedure gen0(op: opcode)

Posted: Fri Jan 02, 2004 4:21 pm
by GedB
Pat,

Purebasic does not have Pascals elaborate typing system, so a direct translation is not possible.

What the code appears to be doing is creating an enumerated type representing assembly opcodes.

Probably the best approach is to create the commands as a set of constants. These constants would then be represented in your codes as long values. This means the variables are declared as default type or with .l.

You will then have to code the features offered by Pascal's typing. For example create an IsInstr() command.

If you really need to use Pascal's typing then why not try FreePascal.

Both Purebasic and FreePascal support DLLs so you can easily mix the two, perhaps using Purebasic for the GUI and FreePascal for the backend.

Posted: Fri Jan 02, 2004 5:07 pm
by patrick88
thanks for your answer,
GedB wrote: If you really need to use Pascal's typing then why not try FreePascal.
it's not my goal, i bought turbo pascal and delphi 2, a long time ago...
if I does that, it is that I desire to know how it goes an compiler/interpreter....

Posted: Fri Jan 02, 2004 5:23 pm
by GedB
In that case, Patrick, what exactly does the code do?

As I say, a direct translation is out of the question. However, you should be able achieve the same results in Purebasic.

Posted: Fri Jan 02, 2004 5:57 pm
by patrick88

Posted: Fri Jan 02, 2004 6:34 pm
by GedB
I'm no Pascal whiz, so the 1995-Snepscheut implementations leave me baffled. Very clever programming, I'm sure.

It looks like you'll have a lot more success with the 1975-Wirth implementation of Pascal-S. These use much more generic code which should prove easier to convert.

Purebasic is a much closer relation to C. Perhaps it would be a good idea to begin by converting one of the C implementations of PL/0 before tackling Pascal-S.

Posted: Fri Jan 02, 2004 9:54 pm
by Danilo
@patrick88:

Code: Select all

Enumeration
  #add : #neg : #mul : #divd : #remd : #div2 : #rem2 : #eqli : #neqi : #lssi
  #leqi: #gtri: #geqi: #dupl : #swap : #andb : #orb  : #load : #stor : #hhalt
  #wri : #wrc : #wrl : #rdi  : #rdc  : #rdl  : #eol  : #ldc  : #ldla : #ldl
  #ldg : #stl : #stg : #move : #copy : #addc : #mulc : #jump : #jumpz: #call
  #adjs: #sets: #exit

  #lastopcode
EndEnumeration

DataSection
  instructions:
    Data$ "add" ,"neg" , "mul","divd","remd","div2", "rem2","eqli", "neqi","lssi"
    Data$ "leqi","gtri","geqi","dupl","swap","andb", "orb" ,"load", "stor","hhalt"
    Data$ "wri" ,"wrc" , "wrl", "rdi", "rdc","rdl" , "eol" , "ldc", "ldla","ldl"
    Data$ "ldg" ,"stl" , "stg","move","copy","addc", "mulc","jump","jumpz","call"
    Data$ "adjs","sets","exit"
EndDataSection

Procedure Init()
  Dim instr.s(#lastopcode-1)
  Restore instructions
  For a = 0 To #lastopcode-1
    Read A$
    instr(a)=A$
  Next a
EndProcedure


Init()

Debug instr(#mul)
Debug instr(#copy)
Debug instr(#exit)
Is this what you wanted ??