Skip to content

Commit

Permalink
* removed Args.fx module; moved argv, arguments() and name() to Sys.
Browse files Browse the repository at this point in the history
* removed Option.fx; moved option back to Builtins
* replaced ',' in record type declaration with ';', which is more natural for C++ users
  • Loading branch information
vpisarev committed Feb 10, 2021
1 parent 6fb0805 commit f663384
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 96 deletions.
6 changes: 3 additions & 3 deletions examples/btree.fx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Args
import Sys

type tree = Empty | Node: {left: tree, right: tree}
type tree = Empty | Node: {left: tree; right: tree}

fun make (d: int) =
if d == 0 { Node {left=Empty, right=Empty} }
Expand All @@ -12,7 +12,7 @@ fun check (t: tree): int {
}

val min_depth = 4
val max_depth = match Args.arguments() {
val max_depth = match Sys.arguments() {
| n_str :: [] => n_str.to_int_or(10)
| _ => 20
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fst.fx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fun find_idx(a: 't [], f: 't -> bool): int
val i3 = find_idx(a, fun (i: int): bool {i < 0})
println(f"excepion-based search: negative number in {a}: {gen_msg(i3, a)}")

type complex_t = {re: float, im: float}
type complex_t = {re: float; im: float}
val c = ref (complex_t {re=1.f, im=1.f})
val d = c->{re=c->re*2, im=c->im*2}
fun abs(c:complex_t) = Math.sqrt(c.re**2 + c.im**2)
Expand Down
4 changes: 2 additions & 2 deletions examples/mandelbrot.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Args
import Sys
import File

val N = match Args.arguments() {
val N = match Sys.arguments() {
| n_str :: [] => n_str.to_int_or(16000)
| _ => 16000
}
Expand Down
6 changes: 3 additions & 3 deletions examples/nbody.fx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Math
val SolarMass = 4.0 * Math.pi * Math.pi
val Year = 365.24

type Vec = {x: double, y: double, z: double}
type Vec = {x: double; y: double; z: double}
operator + (a: Vec, b: Vec) = Vec {x=a.x+b.x, y=a.y+b.y, z=a.z+b.z}
operator - (a: Vec, b: Vec) = Vec {x=a.x-b.x, y=a.y-b.y, z=a.z-b.z}
operator * (a: Vec, s: double) = Vec {x=a.x*s, y=a.y*s, z=a.z*s}
Expand All @@ -12,8 +12,8 @@ fun distance2(a: Vec) = dot(a, a)

type Object =
{
pos: Vec,
vel: Vec,
pos: Vec;
vel: Vec;
mass: double
}

Expand Down
4 changes: 2 additions & 2 deletions examples/rec_test.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type 't point_ = {x: 't, y: 't}
type 't rect_ = {x: 't, y: 't, width: 't, height: 't}
type 't point_ = {x: 't; y: 't}
type 't rect_ = {x: 't; y: 't; width: 't; height: 't}

fun contains(r: 'z rect_, p: 'z point_) =
r.x <= p.x < r.x + r.width &&
Expand Down
4 changes: 2 additions & 2 deletions examples/spectralnorm.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Args, Math
import Sys, Math

fun A(i: int, j: int)
{
Expand Down Expand Up @@ -39,7 +39,7 @@ fun spectralnorm(n: int)
Math.sqrt(vBv/vv)
}

val N = match Args.arguments() {
val N = match Sys.arguments() {
| n_str :: [] => n_str.to_int_or(5500)
| _ => 5500
}
Expand Down
15 changes: 0 additions & 15 deletions lib/Args.fx

This file was deleted.

27 changes: 27 additions & 0 deletions lib/Builtins.fx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ fun assert(f: bool) = if !f {throw AssertError}

fun ignore(_: 't) {}

// 't?, int? etc. can be used instead of 't option, int option ...
module type 't option = None | Some: 't

fun some_or(x: 't?, defval: 't) = match x { | Some(x) => x | _ => defval }
fun getsome(x: 't?) = match x { | Some(x) => x | _ => throw OptionError }
fun isnone(x: 't?) { | Some _ => false | _ => true }
fun issome(x: 't?) { | Some _ => true | _ => false }

fun __is_scalar__(x: 't) = false
fun __is_scalar__(x: int) = true
fun __is_scalar__(x: int8) = true
Expand Down Expand Up @@ -141,6 +149,12 @@ pure fun string(a: double): string = ccode
return fx_ascii2str(buf, -1, fx_result);
}
fun string(a: string) = a

fun string(a: 't?) {
| Some(a) => "Some(" + repr(a) + ")"
| _ => "None"
}

fun ord(c: char) = (c :> int)
fun chr(i: int) = (i :> char)

Expand Down Expand Up @@ -356,6 +370,18 @@ operator .<= (a: ('t...), b: ('t...)): (bool...) = (for aj <- a, bj <- b {aj <=
operator .> (a: ('t...), b: ('t...)): (bool...) = (for aj <- a, bj <- b {aj > bj})
operator .>= (a: ('t...), b: ('t...)): (bool...) = (for aj <- a, bj <- b {aj >= bj})

pure nothrow operator == (a: 't?, b: 't?) {
| (Some(a), Some(b)) => a == b
| (None, None) => true
| _ => false
}
pure nothrow operator <=> (a: 't?, b: 't?) {
| (Some(a), Some(b)) => a <=> b
| (None, Some _) => -1
| (Some _, None) => 1
| _ => 0
}

fun all(a: (bool...)) = fold f=true for x <- a {f & x}
fun any(a: (bool...)) = fold f=false for x <- a {f | x}

Expand Down Expand Up @@ -592,6 +618,7 @@ fun print(a: 't ref) {
fun println() = print("\n")
fun println(a: 't) { print(a); print("\n") }

fun array(): 't [] = [for i<-0:0 {(None : 't?).getsome()}]
fun array(n: int, x: 't) = [for i <- 0:n {x}]
fun array((m: int, n: int), x: 't) = [for i <- 0:m for j <- 0:n {x}]
fun array((m: int, n: int, l: int), x: 't) = [for i <- 0:m for j <- 0:n for k <- 0:l {x}]
Expand Down
6 changes: 3 additions & 3 deletions lib/Map.fx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Below is the original copyright and the license:

* ====
* =====
* Copyright (c) 2007, Benedikt Meurer <benedikt.meurer@googlemail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
Expand Down Expand Up @@ -38,13 +38,13 @@
* be compatible { the Map and Set modules in the OCaml standard library
* (which are implemented using AVL trees). You can use the Rbmap and Rbset
* modules as drop-in replacement for the Map and Set modules.
* ====
* =====
*/

type 'k cmp_t = ('k, 'k) -> int
type color_t = Red | Black
type ('k,'d) tree_t = Empty | Node: (color_t, ('k,'d) tree_t, 'k, 'd, ('k,'d) tree_t)
module type ('k, 'd) map_t = { root: ('k,'d) tree_t, cmp: 'k cmp_t }
module type ('k, 'd) map_t = { root: ('k,'d) tree_t; cmp: 'k cmp_t }

exception RBMapError

Expand Down
29 changes: 0 additions & 29 deletions lib/Option.fx

This file was deleted.

26 changes: 13 additions & 13 deletions lib/Re2.fx
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,30 @@ fun incise(str: string, starts_ends: (int, int)[]): string []
// regex_t - precompiled regexp pattern. Use it for increasing speed, when it's
// needed to check match with same pattern multiple times.
// Equivalent of RE2 objects.
type regex_t = { handle: cptr, find_r: cptr}
type regex_t = { handle: cptr; find_r: cptr}

// replace_pattern_t - precompiled replace pattern. Use it for increasing speed, when it's
// needed to replace many found occurencies.
type replace_piece_t =
| RPInt: int
| RPString: string

type replace_pattern_t = {pieces: replace_piece_t list, max_subnum: int}
type replace_pattern_t = {pieces: replace_piece_t list; max_subnum: int}

// options_t - regular expression options.
// Equivalent of RE2::Options objects. See re2.h for detailed meaning of particular option.
type options_t =
{
posix_syntax: bool = false,
longest_match: bool = false,
// max_mem: int = kDefaultMaxMem,
literal: bool = false,
never_nl: bool = false,
dot_nl: bool = false,
never_capture: bool = false,
case_sensitive: bool = true,
perl_classes: bool = false,
word_boundary: bool = false,
posix_syntax: bool = false;
longest_match: bool = false;
// max_mem: int = kDefaultMaxMem;
literal: bool = false;
never_nl: bool = false;
dot_nl: bool = false;
never_capture: bool = false;
case_sensitive: bool = true;
perl_classes: bool = false;
word_boundary: bool = false;
one_line: bool = false
}

Expand Down Expand Up @@ -707,7 +707,7 @@ fun find_and_consume_n_str(input: string, pos: int, re: regex_t) : (bool ,
// Equivalent of RE2::Anchor
type anchor_t =
{
anchor_start: bool = false,
anchor_start: bool = false;
anchor_both : bool = false // [TODO]: In this case we need enum, but right now anchor_both just overrides anchor_start
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Set.fx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Below is the original copyright and the license:

* ====
* =====
* Copyright (c) 2007, Benedikt Meurer <benedikt.meurer@googlemail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
Expand Down Expand Up @@ -38,13 +38,13 @@
* be compatible { the Map and Set modules in the OCaml standard library
* (which are implemented using AVL trees). You can use the Rbmap and Rbset
* modules as drop-in replacement for the Map and Set modules.
* ====
* =====
*/

type 't cmp_t = ('t, 't) -> int
type color_t = Red | Black
type 't tree_t = Empty | Node: (color_t, 't tree_t, 't, 't tree_t)
module type 't set_t = { root: 't tree_t, size: int, cmp: 't cmp_t }
module type 't set_t = { root: 't tree_t; size: int; cmp: 't cmp_t }

exception RBSetError

Expand Down
11 changes: 11 additions & 0 deletions lib/Sys.fx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@

// Various system services

val argv =
{
pure nothrow fun argc(): int = ccode { return fx_argc() }
pure fun argv(i: int): string = ccode { return fx_cstr2str(fx_argv(i), -1, fx_result) }

[: for i <- 0:argc() {argv(i)} :]
}

fun appname() = List.hd(argv)
fun arguments() = List.tl(argv)

pure nothrow fun getTickCount(): int64 = ccode { return fx_tickcount() }
pure nothrow fun getTickFrequency(): double = ccode { return fx_tickfreq() }
4 changes: 2 additions & 2 deletions lib/UTest.fx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type test_options_t =

type test_info_t =
{
name: string,
name: string;
f: void->void
}

Expand All @@ -44,7 +44,7 @@ fun test_rng_copy(rng: test_rng_t)

type test_state_t =
{
currstatus: bool,
currstatus: bool;
rng: test_rng_t
}

Expand Down
8 changes: 5 additions & 3 deletions src/ast_typecheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,11 @@ and check_exp e env sc =
| _ -> "")
| _ -> ""
in
let m_id = if mstr <> "" then (get_id mstr) else raise ex in
let (ftyp, floc) = mem_ctx in
let new_f = ExpMem(ExpIdent(m_id, (make_new_typ(), floc)), mem_f_exp, mem_ctx) in
let new_f = if mstr = "Builtins" then mem_f_exp else
let m_id = if mstr <> "" then (get_id mstr) else raise ex in
let (ftyp, floc) = mem_ctx in
ExpMem(ExpIdent(m_id, (make_new_typ(), floc)), mem_f_exp, mem_ctx)
in
let new_exp = ExpCall(new_f, r0 :: args0, ctx) in
check_exp new_exp env sc
| _ -> raise ex)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let make_lexer fname =
Parser.IMPORT; Parser.B_STAR; Parser.SEMICOLON]), false)
else
((imports @ [Parser.B_IMPORT; Parser.B_IDENT mname; Parser.SEMICOLON]), false))
([], false) [("Builtins", true); ("Option", true); ("List", false); ("Char", false); ("String", false)] in
([], false) [("Builtins", true); ("List", false); ("Char", false); ("String", false)] in
let tokenbuf = ref imports in
let print_token lexbuf t =
(let s = Lexer.token2str t in
Expand Down
4 changes: 2 additions & 2 deletions src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ iface_decl:
typespec_or_record:
| typespec { $1 }
| LBRACE id_typ_list_ RBRACE { TypRecord {contents=((List.rev $2), true)} }
| LBRACE id_typ_list_ COMMA RBRACE { TypRecord {contents=((List.rev $2), true)} }
| LBRACE id_typ_list_ SEMICOLON RBRACE { TypRecord {contents=((List.rev $2), true)} }

typespec:
| typespec_nf { $1 }
Expand Down Expand Up @@ -1207,7 +1207,7 @@ nobreak_dot_ident:
| IDENT { $1 }

id_typ_list_:
| id_typ_list_ COMMA id_typ_elem { $3 :: $1 }
| id_typ_list_ SEMICOLON id_typ_elem { $3 :: $1 }
| id_typ_elem { $1 :: [] }

id_typ_elem:
Expand Down
4 changes: 2 additions & 2 deletions test/test_all.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Args
import Sys
import UTest
import test_basic
import test_nbody
Expand All @@ -11,5 +11,5 @@ import test_closure
import test_re2
import test_ds

val (run, options) = UTest.test_parse_options(Args.arguments(), "Ficus unit tests.", "")
val (run, options) = UTest.test_parse_options(Sys.arguments(), "Ficus unit tests.", "")
if run {UTest.test_run_all(options)}
Loading

0 comments on commit f663384

Please sign in to comment.