Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f340fea
initial
NekrodNIK Sep 8, 2026
29df19d
Merge branch 'nsu-syspro:main' into main
NekrodNIK Sep 10, 2026
f7c0d79
Merge branch 'nsu-syspro:main' into main
NekrodNIK Sep 20, 2026
368f25f
Merge branch 'nsu-syspro:main' into main
NekrodNIK Sep 21, 2026
7842ba8
lexer and driver
NekrodNIK Sep 10, 2026
49a6e22
refactor lexer
NekrodNIK Sep 14, 2026
63ad284
driver refactor
NekrodNIK Sep 20, 2026
1daa385
fix github actions
NekrodNIK Sep 20, 2026
ede7295
refactor lexer
NekrodNIK Sep 20, 2026
8f4bd14
add UnterminatedMultilineComment error support
NekrodNIK Sep 20, 2026
6d348b0
fix workflow
NekrodNIK Sep 20, 2026
047c8a9
fix exit code
NekrodNIK Sep 20, 2026
08554b9
fix grammar2/*/meta.json
NekrodNIK Sep 20, 2026
5ea8e6c
fix workflow
NekrodNIK Sep 20, 2026
68e9990
fix typo
NekrodNIK Sep 21, 2026
0fb627a
Merge branch 'nsu-syspro:main' into main
NekrodNIK Sep 28, 2026
d7517f8
Merge remote-tracking branch 'origin/main' into grammar1
NekrodNIK Sep 28, 2026
bdafc53
fixed Errors.UnknownToken message
NekrodNIK Sep 28, 2026
b963c07
added a regression test for integer literal limits
NekrodNIK Sep 28, 2026
ab7aa5b
fix integer literal limits bug
NekrodNIK Sep 28, 2026
bd96871
fix
NekrodNIK Sep 28, 2026
f1354a1
refactor: switch to List.t as token container
NekrodNIK Oct 3, 2026
d8b0571
changes dump
NekrodNIK Oct 4, 2026
7c65359
changes dump
NekrodNIK Oct 4, 2026
eb2fed7
something like the parser impl
NekrodNIK Oct 5, 2026
2dc388c
fix github workflow
NekrodNIK Oct 5, 2026
63824ec
refactor syntax
NekrodNIK Oct 7, 2026
c1e0d1d
refactor sema
NekrodNIK Oct 7, 2026
e3dbe15
move all report logic to report.ml
NekrodNIK Oct 7, 2026
06e1088
fix
NekrodNIK Oct 7, 2026
68587e7
enable `integer-literal-limits` test for the parser
NekrodNIK Oct 7, 2026
9ab2031
refactor parser
NekrodNIK Oct 7, 2026
9b6a97a
fix
NekrodNIK Oct 7, 2026
e05498e
Revert "fix"
NekrodNIK Oct 7, 2026
2b54eeb
implement error reporting
NekrodNIK Oct 9, 2026
0d6fd35
refactor sema
NekrodNIK Oct 9, 2026
2b2ec70
driver
NekrodNIK Oct 9, 2026
a8aeb0e
fix driver
NekrodNIK Oct 10, 2026
47a4917
refactor parser
NekrodNIK Oct 10, 2026
d2707c6
refactor
NekrodNIK Oct 10, 2026
a32dc68
Merge branch 'nsu-syspro:main' into main
NekrodNIK Oct 12, 2026
1595e14
Merge remote-tracking branch 'origin/main' into grammar1
NekrodNIK Oct 12, 2026
bc97784
fix test/config.json
NekrodNIK Oct 12, 2026
a50e30e
enable grammar 2 in github workflow
NekrodNIK Oct 13, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# Add grammar versions you have implemented (e.g. [1, 2, 3, 4, 5])
grammar: [1]
grammar: [1, 2]
stage: ["lexer", "parser"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand All @@ -26,16 +26,18 @@ jobs:
- name: Install test harness dependency
run: pip install pytest

- name: Build your compiler
- name: Set-up OCaml
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: 5

- name: Install splc dependencies
run: |
# TODO: Replace with your compiler's build command
# Examples:
# cargo build # Rust
# javac src/*.java -d build/ # Java
# go build -o build/splc ./src # Go
# ghc -o build/splc src/Main.hs # Haskell
echo "No compiler configured. Edit this step to build your compiler."
exit 1
opam install . --deps-only

- name: Build your compiler
run:
opam exec -- dune build

- name: Run tests for grammar ${{ matrix.grammar }}
run: python3 test/run_tests.py test --grammar ${{ matrix.grammar }} --no-build
run: python3 test/run_tests.py test --grammar ${{ matrix.grammar }} --stage ${{ matrix.stage }} --no-build
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ __pycache__/

# OS
.DS_Store
Thumbs.db
Thumbs.db

_opam/
target/
_build/


# Added by cargo

/target
Empty file added .ocamlformat
Empty file.
19 changes: 19 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(lang dune 3.24)

(name splc)

(generate_opam_files true)

(package
(name splc)
(depends
(ocaml
(and
(>= 5.5.0)
(< 5.6.0)))
ppx_string
ppx_deriving
yojson
cmdliner
(ocaml-lsp-server :with-dev-setup)
(ocamlformat :with-dev-setup)))
28 changes: 28 additions & 0 deletions splc.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
depends: [
"dune" {>= "3.24"}
"ocaml" {>= "5.5.0" & < "5.6.0"}
"ppx_string"
"ppx_deriving"
"yojson"
"cmdliner"
"ocaml-lsp-server" {with-dev-setup}
"ocamlformat" {with-dev-setup}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
x-maintenance-intent: ["(latest)"]
59 changes: 59 additions & 0 deletions src/driver/driver.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
open Cmdliner
open Cmdliner.Term.Syntax

let grammar_ver = 1

let splc src_path lr pr =
let src = In_channel.with_open_text src_path In_channel.input_all in
let lex = Spl.Lexer.from_string src in
let ast, parser_errors = Spl.Parser.parse lex in

if Option.is_some lr || Option.is_some pr then
let lexer_flag =
match lr with
| Some lr_path ->
let report, error_flag = Spl.Report.report_lexer lex in
let () =
Out_channel.with_open_text lr_path
@@ (Fun.flip Yojson.Basic.pretty_to_channel) report
in
error_flag
| _ -> false
in

let parser_flag =
match pr with
| Some pr_path ->
let report = Spl.Report.report_ast Fun.id ast in
let () =
Out_channel.with_open_text pr_path
@@ (Fun.flip Yojson.Basic.pretty_to_channel) report
in
not (List.is_empty parser_errors)
| _ -> false
in
if lexer_flag || parser_flag then Cmd.Exit.some_error else Cmd.Exit.ok
else if List.is_empty parser_errors then Cmd.Exit.ok
else
let print_error error =
let () = print_string (Spl.Errors.error_message src_path src error) in
print_newline ()
in
let () = List.iter print_error parser_errors in
Cmd.Exit.some_error

let cmd =
Cmd.v (Cmd.info "splc")
@@
let+ src_path = Arg.(required & pos 0 (some file) None & info [] ~docv:"FILE")
and+ lr = Arg.(value & opt (some path) None & info [ "t" ])
and+ pr = Arg.(value & opt (some path) None & info [ "a" ])
and+ _ =
Arg.(
value
& opt (enum [ ([%string "%{grammar_ver#Int}"], grammar_ver) ]) grammar_ver
& info [ "g" ])
in
splc src_path lr pr

let () = exit (Cmd.eval' cmd)
6 changes: 6 additions & 0 deletions src/driver/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(executable
(public_name splc)
(name driver)
(libraries cmdliner spl)
(preprocess
(pps ppx_deriving.show ppx_string)))
5 changes: 5 additions & 0 deletions src/spl/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name spl)
(libraries yojson)
(preprocess
(pps ppx_deriving.show ppx_string)))
51 changes: 51 additions & 0 deletions src/spl/errors.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type t =
| UnterminatedMultilineComment
| UnknownToken of char
| MissingSemiColon
| Expected of { expected : string; actual : string }
| UndeclaredIdentifier of string
| Redeclaration of string
| ExpressionResultUnused
| AssignToImmutable of string
[@@deriving show]

let to_string = function
| UnterminatedMultilineComment -> "unterminated multi-line comment"
| UnknownToken ch -> [%string "unknown token '%{String.of_char ch}'"]
| MissingSemiColon -> "missing semicolon"
| Expected { expected; actual } ->
[%string "expected '%{expected}', actual: '%{actual}'"]
| UndeclaredIdentifier id -> [%string "undeclared identifier '%{id}'"]
| Redeclaration id -> [%string "redeclaration of %{id}"]
| ExpressionResultUnused -> "expression result unused"
| AssignToImmutable id ->
[%string "cannot assign to immutable variable '%{id}'"]

let underline_length = function
| UnterminatedMultilineComment | UnknownToken _ | MissingSemiColon
| ExpressionResultUnused ->
1
| Expected { actual = s }
| UndeclaredIdentifier s
| Redeclaration s
| AssignToImmutable s ->
String.length s

let red s = [%string "\x1b[31m%{s}\x1b[0m"]
let bold s = [%string "\x1b[1m%{s}\x1b[0m"]

let error_message filepath src (At (loc, err) : t Located.t) =
let lines = String.split_on_char '\n' src in
let line = List.nth lines (loc.line - 1) in
let line_num = Int.to_string loc.line in

let identation = String.make (String.length line_num) ' ' in
let underline = red @@ bold @@ String.make (underline_length err) '^' in
{%string|
%{red @@ bold "error"}: %{to_string err}
%{identation} -> %{filepath}:%{loc.line#Int}:%{loc.col#Int}
%{identation} |
%{ line_num} | %{line}
%{identation} | %{String.make (loc.col-1) ' '}%{underline}
|}
|> String.trim
119 changes: 119 additions & 0 deletions src/spl/lexer.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
open Result.Syntax

type t = { src : string; loc : Located.location }

let from_string src = { src; loc = Located.zero_location }

type 'a res = ('a, Token.t Located.t) result

let peek st : char res =
let { src; loc } = !st in
if loc.offset < String.length src then Ok src.[loc.offset]
else Error (At (loc, Eof))

let peek_string st upper : string =
let { src; loc } = !st in
String.sub src loc.offset (min upper (String.length src - loc.offset))

let eat st : char res =
let { src; loc } = !st in
let+ ch = peek st in
st :=
{
src;
loc =
{
offset = loc.offset + 1;
line = (loc.line + if ch = '\n' then 1 else 0);
col = (1 + if ch = '\n' then 0 else loc.col);
};
};
ch

let rec eat_n st n : unit res =
if n = 0 then Ok ()
else
let* _ = eat st in
eat_n st (n - 1)

let eat_while st pred : string res =
let start_offset = !st.loc.offset in
let rec go x =
match peek st with
| Ok ch when pred ch ->
let* _ = eat st in
go (x + 1)
| _ -> Ok x
in

let+ len = go 0 in
String.sub !st.src start_offset len

let is_digit = Char.Ascii.is_digit
let is_non_digit ch = Char.Ascii.is_letter ch || ch = '_'
let is_whitespace = function ' ' | '\t' | '\r' | '\n' -> true | _ -> false

let rec skip_multiline_comment st start_loc : unit res =
match peek_string st 2 with
| "*/" -> eat_n st 2
| "" -> Error (At (start_loc, Token.Error UnterminatedMultilineComment))
| _ ->
let* _ = eat st in
skip_multiline_comment st start_loc

let rec skip_trivia st : unit res =
let* _ = eat_while st is_whitespace in
match peek_string st 2 with
| "//" ->
let* _ = eat_while st (( <> ) '\n') in
skip_trivia st
| "/*" ->
let start_loc = !st.loc in
let* _ = eat_n st 2 in
let* _ = skip_multiline_comment st start_loc in
skip_trivia st
| _ -> Ok ()

let read_number st : Token.t res =
let+ str = eat_while st is_digit in
Token.Number str

let read_ident st : Token.t res =
let+ str = eat_while st (fun ch -> is_digit ch || is_non_digit ch) in
match str with
| "val" -> Token.Val
| "var" -> Token.Var
| "return" -> Token.Return
| id -> Token.Ident id

let read_token st : Token.t Located.t res =
let loc = !st.loc in
let* ch = peek st in

let single tok = Result.map (fun _ -> tok) (eat st)
and single_err tok = Result.bind (eat st) (fun _ -> Error (At (loc, tok))) in

let token =
match ch with
| '(' -> single Token.LParen
| ')' -> single Token.RParen
| '+' -> single Token.Plus
| '-' -> single Token.Minus
| '/' -> single Token.Slash
| '*' -> single Token.Asterisk
| ';' -> single Token.SemiColon
| '=' -> single Token.Assign
| '0' -> single (Token.Number "0")
| ch when is_digit ch -> read_number st
| ch when is_non_digit ch -> read_ident st
| ch -> single_err (Token.Error (UnknownToken ch))
in
Result.map (fun x -> Located.At (loc, x)) token

let next_token st =
let st' = ref st in
let result =
let* _ = skip_trivia st' in
read_token st'
in
match result with Ok token | Error token -> (token, !st')
3 changes: 3 additions & 0 deletions src/spl/lexer.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type t
val from_string : string -> t
val next_token : t -> Token.t Located.t * t
8 changes: 8 additions & 0 deletions src/spl/located.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type location = { offset : int; line : int; col : int } [@@deriving show]
type 'a t = At of location * 'a [@@deriving show]

let zero_location = {offset=0; line=1; col=1}

let map (f : 'a -> 'b) (At (loc, x) : 'a t) = At (loc, f x)
let get (At (_, x) : 'a t) = x
let get_loc (At (loc, _) : 'a t) = loc
Loading
Loading