Skip to content

Instantly share code, notes, and snippets.

@Chubek
Created June 8, 2024 08:26
Show Gist options
  • Save Chubek/65220b27f5dcc0cd696b84e166b8ab90 to your computer and use it in GitHub Desktop.
Save Chubek/65220b27f5dcc0cd696b84e166b8ab90 to your computer and use it in GitHub Desktop.
Pascal-Syntax.ml: ISO Pascal's syntax formalized in OCaml type system

I have been a lot into formalism lately, and I am trying to sort out my thoughts. Let's just say I am not the sharpest crayon in lil jimmy's lunchbox (which his over-worked mom filled with crayons!).

Pascal-Syntax.ml is an OCaml interface file (which is akin to C's .h files) that defines the syntax of ISO Pascal. You can download the standard from here:

https://www.cs.utexas.edu/users/novak/iso7185.pdf

ISO Pascal differs from widely-used dialects of Pascal (all of which FPC supports!) in a way that it does not support objects, but that is not the only thing it does not support!

Pascal's author, Niklaus Wirth died just a few years ago. It's really sad to see Pascal forgotten. I aim to create a Pascal compiler, or interpreter, or even a translator to another algorithmic language (people often call these 'transpilers' but this is obviously folk colliqualism1).

I think what I can offer would be verification. Or maybe partial evalution, meta-tracing. I shall decide.

This will of course be used to create an AST, or in the parser/lexer.

I am a beginner in all this ('this' meaning life, but also everything else). Please let me know. I am not very apt at ML-like syntax.

chubakbidpaa [at] riseup [dot] net

I am .chubak ( [dot] chubak ) on Discord.

Thanks.

(* Formalism for ISO-7185:1990 "Pascal" Syntax *)
(* These will be extended into an AST *)
(* ISO Pascal is different from Delphi *)
(* Please submit your comments to chubakbidpaa [at] riseup [dot] net *)
type pas_kw = And | Array | Begin | Case | Const
| Div | Do | Downto | Else | End
| File | For | Function | Goto
| If | In | Label | Mod | Nil
| Not | Of | Or | Packed | Procedure
| Program | Record | Repeat | Set
| Then | To | Until | Var | While | With
and pas_val =
Number of float | CharString of string | Id of string
and pas_type_val =
| SimpleType of pas_simple_type
| OrginalType of pas_ordinal_type
| StructuredType of pas_structured_type * bool
| PointerType of string
| FuncType of pas_func_type
| Alias of string
and pas_simple_type = IntegerType | RealType | BoolType | CharType
and pas_ordinal_type =
| Enumerated of string list | Range of (int * int) list
and pas_structured_type =
| Array of pas_array_unit list
| Record of {
fixed_part: pas_rec_fixed_part list option;
variant_part: pas_rec_variant_part list option;
}
| Set of pas_type_val
| File of pas_type_val
and pas_array_unit =
{ index_type: pas_type_val; component_type: string; }
and pas_rec_fixed_part = { names: string list;
typs: pas_type_val list; }
and pas_rec_variant_part = { selector: (string * string option);
variants: (string list * pas_type_val) list; }
and pas_func_type = { name: string;
params: pas_param list;
result_type: pas_type_val; }
and pas_param = { name: string option; typ: pas_type_val; }
and pas_block = LabelDecl of pas_label list
| ConstDecl of pas_const list
| TypeDecl of pas_type list
| VarDecl of pas_var list
| ProcDecl of pas_proc list
| FuncDecl of pas_func list
| StmtDecl of pas_stmt list
and pas_label = { name: string; value: int }
and pas_const = { name: string; value: pas_val; typ: pas_type_val; }
and pas_var = | Basic of { name: string; points: bool; index: pas_val list; }
| Long of pas_var list
and pas_type = { name: string; value: pas_type_val; }
and pas_proc = { name: string; params: pas_param list; body: pas_stmt list; }
and pas_func = { typ: pas_func_type; body: pas_stmt list; }
and pas_stmt = | BlockStmt of pas_block
| Expression of pas_expr
| Controlflow of pas_control
| Assignment of pas_var * pas_expr
and pas_expr = | Var of pas_var
| Call of pas_var * pas_val list
| Infix of { left: pas_expr list;
right: pas_expr list;
operator: pas_infixop; }
| Prefix of pas_prefixop * pas_expr
and pas_infixop = Add | Sub | Mul | Div | IDiv | Mod | Eq | Ne | Lt | Le
| Gt | Ge | BitAnd | BitOr | Xor | And | Or | Union
| Diff | Inter | SymDiff | Incl | Cat
and pas_prefixop = UMin | UPos | BitNot | Deref | AddrOf
and pas_control = | If of { cond: pas_expr;
branches: (pas_expr * pas_block) list;
clause: pas_block }
| Case of { disc: pas_expr;
clauses: (pas_expr * pas_block);
default: pas_block; }
| Loop of { starter: pas_stmt;
ender: pas_stmt;
ranger: pas_kw;
block: pas_block;
}
| While of { cond: pas_expr;
block: pas_block; }
| RepeatUntil of { block: pas_block; cond: pas_expr; }
| With of { var: pas_var; block: pas_block; }
| Goto of string
and pas_program = { name: string; blocks: pas_block list; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment