Skip to content

Commit

Permalink
Fixed AST; parser and lexer full implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
KabirSamsi committed Apr 21, 2024
1 parent fe82c87 commit a7e8256
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_build
24 changes: 1 addition & 23 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
(lang dune 3.14)

(name KLaTsL)

(generate_opam_files true)

(source
(github KabirSamsi/KLaTsL))

(authors "Kabir Samsi")

(maintainers "Kabir Samsi")

(license LICENSE)

(documentation https://url/to/documentation)

(package
(name KLaTsL)
(synopsis "LinAlg Interpreter!")
(description "Linear algebra language interpreter and typesetting framework allowing to write, typeset and create programs to solve linear algebra problems and compile solutions to LaTeX.")
(depends ocaml dune)
(tags
(some placeholders)))

(using menhir 3.0)
51 changes: 33 additions & 18 deletions src/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,44 @@ type factorization = LU | CR | PDP | QR | SVD;;

type varname = string

type uop =
| Read | Not | Len | Id | Det | Dim | Square | Inv
| Ref | Rref | Span | Transpose | Abs | Norm

type bop =
| Plus | Minus | Times | Divide | Pow | Cross
| Diff | Range | Get | Eq | Gt | Geq | Lt | Leq
| Implies | Iff | Solve | Change | Fac

type top =
| Set | Gen | Squeeze

type expr =
(* Baseline Types *)
| EEmpty
| EInt of int
| EFloat of float
| EBool of bool
(* Terminals Types *)
| Unit
| Int of int
| Float of float
| Bool of bool

| EVector of (expr * expr)
| EMatrix of (expr * expr)
| ESpace of (expr * expr)
| ERange of (expr * expr)
(* Variables *)
| Var of varname

(* Linear Algebra structures *)
| Vector of expr list
| Space of expr list
| Range of expr * expr

(* Operators *)
| EUopExpr of uop * expr
| EBopExpr of bop * expr * expr
| UExpr of uop * expr
| BExpr of bop * expr * expr
| TExpr of top * expr * expr * expr

(* NOTE: Turn into unary operator *)
| EFactorization of factorization * expr
(* Ifs and Loops *)
| If of expr * expr * expr
| While of expr * expr
| For of varname * expr * expr * expr

(* Definitions And Functions *)
| Assgn of varname * expr
| Let of varname * expr * expr

and uop =
Neg | Square | Transpose | Norm | Det | Inverse | REF | RREF | Eigenvalues | Eigenvectors | Orth

and bop = Add | Subtract | Multiply | Divide | Eq | Neq | Gt | Lt | Geq | Leq | Power | Solve
| Fun of expr list * expr
10 changes: 7 additions & 3 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
(library
(name klatsl)
(public_name KLaTsL)
(libraries str menhirLib))
(name klatsl)
(public_name KLaTsL))

(menhir
(modules parser))

(ocamllex lexer)
4 changes: 2 additions & 2 deletions src/eval.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
open Ast
(* open Ast
exception TypeError of string
exception DimensionError
Expand Down Expand Up @@ -323,4 +323,4 @@ and det v = failwith "Unimplemented"
and orth v = failwith "Unimplemented"
and eigenvalues v = failwith "Unimplemented"
and eigenvectors v = failwith "Unimplemented"
and factor f e = failwith "Unimplemented"
and factor f e = failwith "Unimplemented" *)
44 changes: 0 additions & 44 deletions src/eval.mli

This file was deleted.

40 changes: 30 additions & 10 deletions src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,91 @@ let int = '-'? digit+
let float = '-'? digit+'.'digit+
let letter = ['a'-'z' 'A'-'Z']
let var = letter+
let sep = ',' ', '?
let sep = ',' ' '?

rule read = parse
| white { read lexbuf }

(* Binary Operators *)
| "^" { POW }
| "*" { TIMES }
| "x" { CROSS }
| "/" { DIVIDE }
| "+" { PLUS }
| "-" { MINUS }
| "\\" { DIFF }
| "range" { RANGE }
| "get" { GET }
| "(" { LPAREN }
| ")" { RPAREN }
| "->" { ARROW }
| "=>" { IMPLIES }
| "<=>" { IFF }
| "=" { EQUALS }
| ">" { GT }
| ">=" { GEQ }
| "<" { LT }
| "<=" { LEQ }
| "let" { LET }
| "in" { IN }
| "solve" { SOLVE }
| "change" { CHANGE }
| "factor" { FAC }

(* Vector & Space Declarations *)
| "[" { LBRACKET }
| "]" { RBRACKET }
| "{" { LBRACE }
| "}" { RBRACE }
| sep { SEP }

(* Unary Operators *)
| "read" { READ }
| "not" { NOT }
| "len" { LEN }
| "id" { ID }
| "det" { DET }
| "inv" { INV }
| "dim" { DIM }
| "square" { SQUARE }
| "ref" { REF }
| "rref" { RREF }
| "factor" { FAC }
| "span" { SPAN }
| "^tsp" { TRANSPOSE }
| "|" { ABS }
| "||" { NORM }

(* Ternary Operators *)
| "set" { SET }
| "generate" { GEN }
| "squeeze" { SQUEEZE }

| "true" { TRUE }
| "false" { FALSE }
(* Let & Functions *)
| "let" { LET }
| "in" { IN }
| "fun" { FUN }
| "->" { ARROW }

(* If-Else *)
| "if" { IF }
| "then" { THEN }
| "else" { ELSE }

(* Loops *)
| "while" { WHILE }
| "do" { DO }
| "for" { FOR }

| "fun" { FUN }

(* Factorizations *)
| "QR" { QR }
| "SVD" { SVD }
| "PDP" { PDP }
| "CR" { CR }
| "LU" { LU }

(* Terminals *)
| "true" { TRUE }
| "false" { FALSE }
| "()" { UNIT }
| var { VAR (Lexing.lexeme lexbuf)}
| ":" { TYP }
| int { INT (int_of_string (Lexing.lexeme lexbuf)) }
| float { FLOAT (float_of_string (Lexing.lexeme lexbuf)) }
| var { VAR (Lexing.lexeme lexbuf)}
| eof { EOF }
Loading

0 comments on commit a7e8256

Please sign in to comment.