-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental support for macro "functions"
- Loading branch information
Showing
6 changed files
with
174 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/Visp.Compiler.UnitTests/snapshots/tests_macros_struct-macro-2.can parse.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This file is auto-generated | ||
|
||
#nowarn "0020" // unused results from functions | ||
|
||
open Visp.Runtime.Library | ||
|
||
let state = { Todo = () } | ||
// line 8 @"struct-macro-2.visp" | ||
let macro_MyStruct2 = "__MACRO_INIT__" | ||
// line 22 @"struct-macro-2.visp" | ||
|
||
[<Struct()>] | ||
// line 22 @"struct-macro-2.visp" | ||
type Example (x: int, y: int) = | ||
// line 22 @"struct-macro-2.visp" | ||
member _.X = | ||
x | ||
// line 22 @"struct-macro-2.visp" | ||
member _.Y = | ||
y | ||
// line 22 @"struct-macro-2.visp" | ||
member d.Sum () = | ||
// line 22 @"struct-macro-2.visp" | ||
CoreMethods.``add``((d.X), (d.Y)) | ||
|
||
// line 22 @"struct-macro-2.visp" | ||
let mkExample x y = | ||
// line 22 @"struct-macro-2.visp" | ||
(new Example(x, y)) | ||
|
||
// line 28 @"struct-macro-2.visp" | ||
let instance = | ||
// line 28 @"struct-macro-2.visp" | ||
mkExample (1) (2) | ||
// line 30 @"struct-macro-2.visp" | ||
printfn ("Example Struct is %A") (instance) | ||
// line 31 @"struct-macro-2.visp" | ||
printfn ("Example IsValueType %A") (instance | ||
|> (fun a1 -> | ||
// line 31 @"struct-macro-2.visp" | ||
(a1.GetType())) | ||
|> (fun a1 -> | ||
// line 31 @"struct-macro-2.visp" | ||
(a1.IsValueType))) | ||
// line 32 @"struct-macro-2.visp" | ||
let visp_result_todo = | ||
// line 32 @"struct-macro-2.visp" | ||
printfn ("Example Result is %i") ((instance.Sum())) | ||
// line 32 @"struct-macro-2.visp" | ||
printfn ("%A") (visp_result_todo) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
tests/Visp.ExecutionTests/snapshots/tests_macros_struct-macro-2.can execute.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Example Struct is struct-macro-2+Example | ||
Example IsValueType true | ||
Example Result is 3 | ||
() | ||
|
||
ExitCode: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
;; Copyright 2023 Ville Penttinen | ||
;; Distributed under the MIT License. | ||
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md | ||
;; | ||
;; for basic syntax highlighting | ||
;; vim: set syntax=clojure: | ||
|
||
(syntax-macro MyStruct2 | ||
[(_ typ (arg ctor ...) body ...) | ||
(begin | ||
(#[Struct] | ||
type typ (arg ctor ...) | ||
body ... | ||
) | ||
|
||
(fn (m-concat-id mk typ) | ||
((m-map m-name (arg ctor ...))) | ||
(new typ (m-map m-name (arg ctor ...)))) | ||
) | ||
]) | ||
|
||
(MyStruct2 Example ([x: int] [y: int]) | ||
(member _.X x) | ||
(member _.Y y) | ||
|
||
(member fn d.Sum () (+ (+X d) (+Y d)))) | ||
|
||
(let instance (mkExample 1 2)) | ||
|
||
(printfn "Example Struct is %A" instance) | ||
(printfn "Example IsValueType %A" (->> instance .GetType +IsValueType)) | ||
(printfn "Example Result is %i" (.Sum instance)) |