Skip to content

Commit

Permalink
Makes possible to create arrays of complex types in Lua and Js
Browse files Browse the repository at this point in the history
  • Loading branch information
modlfo committed Apr 24, 2020
1 parent 66aeb4e commit 6d2b82d
Show file tree
Hide file tree
Showing 73 changed files with 96 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ if-then-else=fit-or-vertical
break-cases=all
comment-check=false
wrap-comments=false
doc-comments-padding=3
doc-comments-padding=3
doc-comments=before
2 changes: 1 addition & 1 deletion src/core/args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type code =
| JavaCode
[@@deriving show, eq, ord]

(** Stores the options passed to the command line *)
type args =
{ mutable files : input list
; mutable dparse : bool
Expand All @@ -66,7 +67,6 @@ type args =
; mutable prefix : string
}
[@@deriving show, eq, ord]
(** Stores the options passed to the command line *)

let default_arguments : args =
{ files = []
Expand Down
1 change: 1 addition & 0 deletions src/core/env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ let builtin_table extensions =
; [ "get" ], Scope.Function, Typ.Const.array_get (), false
; [ "size" ], Scope.Function, Typ.Const.array_size (), false
; [ "makeArray" ], Scope.Function, Typ.Const.array_make (), false
; [ "makeComplexArray" ], Scope.Function, Typ.Const.array_make (), false
; [ "abs" ], Scope.Function, Typ.Const.freal_freal (), false
; [ "exp" ], Scope.Function, Typ.Const.freal_freal (), false
; [ "log10" ], Scope.Function, Typ.Const.freal_freal (), false
Expand Down
2 changes: 1 addition & 1 deletion src/core/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ module Env = struct
; tick : int
}

type env = t list
(** Non empty list of 't' *)
type env = t list

let new_t () =
{ locals = [ SimpleMap.create 4 ]
Expand Down
1 change: 1 addition & 0 deletions src/generators/codeJs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Templates = struct
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }
|}
Expand Down
13 changes: 12 additions & 1 deletion src/generators/codeLua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end
|pla}

Expand Down Expand Up @@ -229,16 +230,22 @@ end
|pla}


let simple (_ : config) _ code = {pla|
<#env#>
<#code#>
|pla}

let apply params (module_name : string) (template : string) (code : Pla.t) : (Pla.t * FileKind.t) list =
match template with
| "default" -> [ default params.config module_name code, FileKind.ExtOnly "lua" ]
| "performance" -> Performance.getLua params (default params.config module_name code)
| "vcv-prototype" -> [ vcv params.config module_name code, FileKind.ExtOnly "lua" ]
| "simple" -> [ simple params.config module_name code, FileKind.ExtOnly "lua" ]
| _ -> [ none code, FileKind.ExtOnly "lua" ]
end

let isSpecial (params : params) (name : string) : bool =
if params.template <> "vcv-prototype" then
if params.template = "default" then
match name with
| _ when name = params.module_name ^ "_process" -> true
| _ when name = params.module_name ^ "_noteOn" -> true
Expand Down Expand Up @@ -379,6 +386,10 @@ let rec printSwitchStmt params e cases def =

and printStmt (params : params) (stmt : cstmt) : Pla.t option =
match stmt with
| CSBind (CLId (_, name), CECall ("makeComplexArray", [ size; CECall (init, [], _) ], _)) ->
let name = dot name in
let size_t = printExp params size in
Some {pla|<#name#> = makeComplexArray(<#size_t#>,<#init#s>);|pla}
| CSVar (CLWild, None) -> None
| CSVar (CLId (typ, name), None) ->
let init = getInitValue typ in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ let checkConfig (config : config) (args : args) =
let () = checksForVCVPrototype config args in
if
(args.code = CCode && args.template <> "default")
|| (args.code = LuaCode && args.template <> "vcv-prototype")
|| (args.code = LuaCode && args.template = "default")
|| args.code = JSCode
then
if
Expand Down
3 changes: 2 additions & 1 deletion src/generators/progToCode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ let rec convertStmt (p : parameters) (s : stmt) : cstmt =
let attr = { is_root } in
CSFunction (convertTypeMakeTupleUnit p ret, fname, arg_names, body', attr)
(* special case for c/c++ to replace the makeArray function *)
| StmtBind (LWild _, PCall (NoInst, [ "makeArray" ], [ size; init; var ], attr), _) when p.code = CCode ->
| StmtBind (LWild _, PCall (NoInst, [ ("makeArray" | "makeComplexArray") ], [ size; init; var ], attr), _)
when p.code = CCode ->
let init' = convertExp p init in
let size' = convertExp p size in
let init_typ = expType p init in
Expand Down
4 changes: 2 additions & 2 deletions src/generators/templates/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type output =
| OInt
| OBool

(** Represents the 'plugin' configuration *)
type config =
{ module_name : string
; process_inputs : input list
Expand All @@ -47,13 +48,13 @@ type config =
; update_outputs : output list
; update_found : bool
}
(** Represents the 'plugin' configuration *)

type target_file =
| Implementation
| Header
| Tables

(** Represents the parameters used during code generation *)
type params =
{ real : string (** 'Real' number representation *)
; template : string (** Used template *)
Expand All @@ -64,7 +65,6 @@ type params =
; prefix : string (** Prefix given to java code *)
; config : config (** Plugin configuration *)
}
(** Represents the parameters used during code generation *)

(** Empty default configuration *)
let empty_conf module_name =
Expand Down
2 changes: 1 addition & 1 deletion src/generators/templates/max.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
THE SOFTWARE.
*)

open Config
(** Template for the Teensy Audio library *)
open Config

let tables (params : params) (code : Pla.t) : Pla.t =
let file = String.uppercase params.output in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/templates/pd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
THE SOFTWARE.
*)

open Config
(** Template for the Teensy Audio library *)
open Config

let tables (params : params) (code : Pla.t) : Pla.t =
let file = String.uppercase params.output in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/templates/teensyAudio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
THE SOFTWARE.
*)

open Config
(** Template for the Teensy Audio library *)
open Config

let inputsArray n_inputs =
if n_inputs > 0 then
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ptypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type 'kind token =
; loc : Loc.t
}

(** Type containing the stream of tokens *)
type 'kind stream =
{ lexbuf : Lexing.lexbuf
; mutable has_errors : bool
Expand All @@ -84,4 +85,3 @@ type 'kind stream =
; mutable prev : 'kind token
; lines : lexed_lines
}
(** Type containing the stream of tokens *)
4 changes: 2 additions & 2 deletions src/parser/tokenStream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

open Ptypes

exception ParserError of Error.t
(** Parsing exception *)
exception ParserError of Error.t

module type TokenKindSig = sig
type kind
Expand All @@ -42,6 +42,7 @@ module type TokenKindSig = sig
end

module TokenStream (S : TokenKindSig) = struct
(** Type containing the stream of tokens *)
type stream =
{ lexbuf : Lexing.lexbuf
; mutable has_errors : bool
Expand All @@ -50,7 +51,6 @@ module TokenStream (S : TokenKindSig) = struct
; mutable prev : S.kind token
; source : Loc.source
}
(** Type containing the stream of tokens *)

let backup (s : stream) =
let lexbuf =
Expand Down
9 changes: 7 additions & 2 deletions src/passes/pass3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ module CreateInitFunction = struct
| Typ.TId ([ "bool" ], _) -> PBool (false, typedAttr)
| Typ.TId (name, _) -> PCall (NoInst, getInitFunctioName name, [], typedAttr)
| Typ.TComposed ([ "array" ], [ sub; { contents = Typ.TInt (size, _) } ], _) ->
let sub_init = getInitValue sub in
let init_sub = getInitValue sub in
let intTypeAttr = { emptyAttr with typ = Some Typ.Const.int_type } in
PCall (NoInst, [ "makeArray" ], [ PInt (size, intTypeAttr); sub_init ], typedAttr)
let make_function =
match init_sub with
| PCall _ -> "makeComplexArray"
| _ -> "makeArray"
in
PCall (NoInst, [ make_function ], [ PInt (size, intTypeAttr); init_sub ], typedAttr)
| Typ.TComposed ([ "tuple" ], types, _) ->
let elems = List.map getInitValue types in
PTuple (elems, typedAttr)
Expand Down
4 changes: 2 additions & 2 deletions src/util/loc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
THE SOFTWARE.
*)

open Lexing
(** Type to hold the location *)
open Lexing

(** Source of the code *)
type source =
| File of string
| Text of string

(** Location information *)
type t =
{ start_pos : position
; end_pos : position
; source : source
}
(** Location information *)

let pp fmt _ = Format.pp_print_string fmt "loc"

Expand Down
2 changes: 1 addition & 1 deletion src/util/printBuffer.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(** String buffer with a few useful functions *)
type print_buffer =
{ buffer : Buffer.t
; mutable indent : int
; mutable space : string
; mutable insert : bool
}
(** String buffer with a few useful functions *)

(** Creates a print buffer *)
let makePrintBuffer () = { buffer = Buffer.create 100; indent = 0; space = ""; insert = false }
Expand Down
1 change: 1 addition & 0 deletions test/code/ad.lua.base
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end

function Util__ctx_type_0_init()
Expand Down
1 change: 1 addition & 0 deletions test/code/adsr.lua.base
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end

function Util__ctx_type_0_init()
Expand Down
1 change: 1 addition & 0 deletions test/code/af_f.js.base.browser
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function vultProcess() {
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/af_f.js.base.webaudio
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/aff_f.js.base.browser
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function vultProcess() {
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/aff_f.js.base.webaudio
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/aff_ff.js.base.browser
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function vultProcess() {
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/aff_ff.js.base.webaudio
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/afi_fi.js.base.browser
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function vultProcess() {
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/afi_fi.js.base.webaudio
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.int_to_float = function(i) { return i; };
this.float_to_int = function(i) { return Math.floor(i); };
this.makeArray = function(size, v){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
this.makeComplexArray = function(size, f){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=f(); return a; };
this.wrap_array = function(a) { return a; }
this.log = function(x) { console.log(x); }

Expand Down
1 change: 1 addition & 0 deletions test/code/ahr.lua.base
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end

function Util__ctx_type_0_init()
Expand Down
2 changes: 1 addition & 1 deletion test/code/array_defined_type.java.base
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private Array_defined_type_dot Array_defined_type_dot_init(){

private Array_defined_type__ctx_type_0 Array_defined_type__ctx_type_0_init(){
Array_defined_type__ctx_type_0 _ctx = new Array_defined_type__ctx_type_0();
_ctx.p1 = makeArray(12,Array_defined_type_dot_init());
_ctx.p1 = makeComplexArray(12,Array_defined_type_dot_init());
return _ctx;
}

Expand Down
2 changes: 1 addition & 1 deletion test/code/array_defined_type.js.base
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ this.Array_defined_type_dot_init = function(){
}
this.Array_defined_type__ctx_type_0_init = function(){
var _ctx = {};
_ctx.p1 = this.makeArray((12|0),this.Array_defined_type_dot_init());
_ctx.p1 = this.makeComplexArray((12|0),this.Array_defined_type_dot_init());
return _ctx;
}
this.Array_defined_type_foo_init = function() { return this.Array_defined_type__ctx_type_0_init(); }
Expand Down
3 changes: 2 additions & 1 deletion test/code/array_defined_type.lua.base
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end

function Array_defined_type_dot_init()
Expand All @@ -33,7 +34,7 @@ end

function Array_defined_type__ctx_type_0_init()
local _ctx = {};
_ctx.p1 = makeArray(12,Array_defined_type_dot_init());
_ctx.p1 = makeComplexArray(12,Array_defined_type_dot_init);
return _ctx;

end
Expand Down
1 change: 1 addition & 0 deletions test/code/bitcrush.lua.base
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sqrt(x) return x; end
function set(a, i, v) a[i+1]=v; end
function get(a, i) return a[i+1]; end
function makeArray(size, v) local a = {}; for i=1,size do a[i]=v end return a; end
function makeComplexArray(size, f) local a = {}; for i=1,size do a[i]=f() end return a; end
function wrap_array(a) return a; end

local Bitcrush_factor_c0 = {24.7875218,24.7873664,24.7867582,24.7854209,24.783098,24.7795519,24.7745634,24.7679304,24.759467,24.7490033,24.7363839,24.7214679,24.7041273,24.6842473,24.6617249,24.6364687,24.6083981,24.5774428,24.5435421,24.5066447,24.466708,24.4236974,24.3775862,24.328355,24.2759912,24.2204889,24.1618479,24.1000739,24.0351781,23.9671764,23.8960897,23.8219429,23.7447654,23.6645899,23.581453,23.4953943,23.4064567,23.3146856,23.2201289,23.1228371,23.0228627,22.92026,22.8150852,22.7073961,22.5972518,22.4847126,22.3698401,22.2526967,22.1333455,22.0118507,21.8882765,21.7626881,21.6351506,21.5057297,21.374491,21.2415003,21.1068232,20.9705254,20.8326722,20.6933287,20.5525599,20.41043,20.2670032,20.1223427,19.9765116,19.8295721,19.6815859,19.532614,19.3827165,19.2319531,19.0803824,18.9280622,18.7750495,18.6214006,18.4671707,18.3124141,18.1571843,18.0015336,17.8455136,17.6891749,17.532567,17.3757384,17.2187367,17.0616084,16.9043992,16.7471535,16.5899149,16.4327259,16.275628,16.1186617,15.9618665,15.8052808,15.6489422,15.4928871,15.3371511,15.1817687,15.0267734,14.8721979,14.7180736,14.5644314,14.411301,14.2587112,14.1066899,13.955264,13.8044598,13.6543024,13.5048162,13.3560246,13.2079502,13.060615,12.9140399,12.7682451,12.6232499,12.479073,12.3357322,12.1932446,12.0516265,11.9108935,11.7710606,11.6321419,11.494151,11.3571007,11.2210031,11.0858699,10.9517118,10.8185393,10.686362,10.5551889};
Expand Down
Loading

0 comments on commit 6d2b82d

Please sign in to comment.