SlideShare a Scribd company logo
1 of 18
Download to read offline
Macros in Nemerle
Parser Enthusiast
Kota Mizushima(@kmizu)
2
Self Introduction
Twitter ID: @kmizu
GitHub: https://github.com/kmizu
Like: PEG, Nemerle, Scala, etc.
Interested In: Formal Languages, Parsing
3
What's Nemerle?(1)
Object & functional programming language
➢On .NET Framework (CLR or Mono)
From: 2003
➢Old
Original Developer: Kamil Skalski, etc.
Last stable: v1.2.503.0 ( 2016-03-15 )
4
What's Nemerle?(2)
Macro
Powerful type inference
➢Can infer type from call-site
Pattern matching & variant
First-class function
, etc.
5
Powerful Type Inferences
There exist type inferences in recent languages.
Nemerle's type inference is different from:
def fact(n){
| 0 => 1
| _ => n * fact(n - 1)
} // recursive function ( without type annotation )
fact(4) // int -> int is inferred from use-site
6
What's Macro?
Features so-called cacros
➢Lisp's macro
➢Lisp's reader macro
➢Preprocessor macro (in C family language)
➢Hidemaru Macro ← Different meaning
➢Excel Macro(VBA) ← Different meaning
Nemerle's macro is inspired by Lisp's macro
7
What's Nemerle's Macros?
Lexical level macros
➢[Token] -> [Token]
AST level macros
➢ASTNode -> ASTNode
Custom attribute macros
➢Global rewriting of program is allowed using such macros
➢Compile time MOP
8
AST Level Macros
Similar with Lisp's macros
Receive AST of expression and return AST
➢Transformation is written in Nemerle itself
➢Typed AST is handled
Many language constructs is just macros
➢If, while, for, using, printf, lock, etc.
9
Hello, World! In Nemerle
Statically checked printf
using Nemerle.IO;
printf("%d + %d = %dn", 1, 2, 1 + 2) //OK
printf("%d + %d = %dn", 1, 2, "3") //NG
printf("%d + %d = %dn", 1, 2, 3.0) //NG
def format = "%d + %d = %dn"
printf(format, 1, 2, 3) //NG. format must be string literal
10
Using Macros(1)
using System;
macro fact(n: int) {
def loop(m) {//type inference for recursive function
| 0 => 1
| _ => m * f(m - 1)
}
{ def r = f(n); Console.WriteLine("fact({0}" = {1}", n, r); <[ $(r: int) ]> }
}
fact(4) // → 24
11
Using Macros(2)
using System.Text.RegularExpressions;
using Nemerle.Compiler
macro regex(n: String) {
def r = <[ Regex($(n: string)) ]>;
try { _ = Regex(n); r; } catch { | _ is ArgumentException =>
Message.Warning("illegal pattern " + n); r }
}
regex(".*") // OK
regex("**") // → "illegal pattern **" in compile time
12
Using Macros(3)
Swap macros
macro swap(n, m) syntax("swap", n, "and", m) {// new syntax: swap n and m
<[ def tmp = $n; $n = $m; $m = tmp; ]>
}
using System;
mutable x = 10; mutable y = 20;
swap x and y; // diffent syntax
Console.WriteLine("({0}, {1})", x, y); // → "(20, 10)"
13
Solving Maze in Compile Time(1)
using System; using System.Console;
using System.IO.File; using Nemerle.Collections;
using Nemerle.Imperative; using Nemerle.Compiler;
macro SolveMaze(inputFile: String) {
def maze = ReadAllLines(inputFile)
def h = maze.length;
...
}
14
Solving Maze in Compile Time(2)
**************************
*S* * *
*** * * ************* *
* * * ************ *
* * *
************** ***********
* *
** ***********************
* * G *
* * *********** * *
* * ******* * *
* * *
**************************
**************************
*S* * $$$$ *
*$* *$$* $************* *
*$* $$* $$************ *
*$$$$* $$$$$ *
**************$***********
* $$$$$$$$$$$$$ *
**$***********************
* $$$$$* $$$$$$$$$$$$$G *
* * $$$$*********** * *
* * ******* * *
* * *
**************************
15
Custome Attribute Macro(Compile Time MOP)
using Nemerle.Compiler; using Nemerle.Collections;
namespace DesignPatterns {
[Nemerle.MacroUsage
(Nemerle.MacroPhase.WithTypedMembers,
Nemerle.MacroTargets.Field)]
macro Proxy (t : TypeBuilder, f : FieldBuilder, iface) { ... }
}
using DesignPatterns;
class Point { public X : int { ... }; public Y() : int { ... }; }
class PointProxy {
[Proxy()] //invocation of Proxy macro
point_ : Point;
}
16
Lexical Level Macros
macro JSONData (group : Token)
syntax ("json", group) {
...
}
...
json({"languages":[
"Java", "Scala", "Nemerle", "C#", "Haskell", "OCaml"
]}) //custom lexer is used
17
Builtin Custom Attribute Macros
ProxyPublicMembers macro
➢also known as first class delegation
Lazy macro
➢Lazy evalution
Memoize macro
Etc.
18
Concolusion
Overview of Nemerle's macros
➢Lexical level macros
➢AST level macros
➢Custom attribute macros
Nemerle is awesome!
Try Nemerle!
➢http://nemerle.org

More Related Content

What's hot

20171010 on-box programmability
20171010 on-box programmability20171010 on-box programmability
20171010 on-box programmabilityKazumasa Ikuta
 
Scala user-group-19.03.2014
Scala user-group-19.03.2014Scala user-group-19.03.2014
Scala user-group-19.03.2014Jan Herich
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
Manipulating string data with a pattern in R
Manipulating string data with  a pattern in RManipulating string data with  a pattern in R
Manipulating string data with a pattern in RLun-Hsien Chang
 
27.2.9 lab regular expression tutorial
27.2.9 lab   regular expression tutorial27.2.9 lab   regular expression tutorial
27.2.9 lab regular expression tutorialFreddy Buenaño
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Kel Cecil
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talkJohn Stevenson
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RLun-Hsien Chang
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Kamal Acharya
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel ZikmundKarel Zikmund
 
เมธอด ชั้น ม 6 ห้อง 2
เมธอด ชั้น ม  6 ห้อง 2เมธอด ชั้น ม  6 ห้อง 2
เมธอด ชั้น ม 6 ห้อง 2Pookie Pook
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of GoFrank Müller
 

What's hot (20)

20171010 on-box programmability
20171010 on-box programmability20171010 on-box programmability
20171010 on-box programmability
 
Scala user-group-19.03.2014
Scala user-group-19.03.2014Scala user-group-19.03.2014
Scala user-group-19.03.2014
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
 
Manipulating string data with a pattern in R
Manipulating string data with  a pattern in RManipulating string data with  a pattern in R
Manipulating string data with a pattern in R
 
27.2.9 lab regular expression tutorial
27.2.9 lab   regular expression tutorial27.2.9 lab   regular expression tutorial
27.2.9 lab regular expression tutorial
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talk
 
Secure code 3rd_party_libs
Secure code 3rd_party_libsSecure code 3rd_party_libs
Secure code 3rd_party_libs
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 
Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in R
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
 
Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!
 
เมธอด ชั้น ม 6 ห้อง 2
เมธอด ชั้น ม  6 ห้อง 2เมธอด ชั้น ม  6 ห้อง 2
เมธอด ชั้น ม 6 ห้อง 2
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of Go
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 

Similar to Macros in nemerle

Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to ElixirDiacode
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with ClojureJohn Stevenson
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John StevensonJAX London
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingRuymán Reyes
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako, Vasil Remeniuk
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Javakim.mens
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency ConstructsTed Leung
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in PracticeAlina Dolgikh
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already KnowKevlin Henney
 

Similar to Macros in nemerle (20)

Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous Computing
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 

More from Kota Mizushima

ドワンゴにおける新卒エンジニア向けScala研修について
ドワンゴにおける新卒エンジニア向けScala研修についてドワンゴにおける新卒エンジニア向けScala研修について
ドワンゴにおける新卒エンジニア向けScala研修についてKota Mizushima
 
株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状Kota Mizushima
 
Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -Kota Mizushima
 
Scalaの現状と今後
Scalaの現状と今後Scalaの現状と今後
Scalaの現状と今後Kota Mizushima
 
Scala Performance Tuning Tips
Scala Performance Tuning TipsScala Performance Tuning Tips
Scala Performance Tuning TipsKota Mizushima
 
こわくない型クラス
こわくない型クラスこわくない型クラス
こわくない型クラスKota Mizushima
 
About Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and BorrowingAbout Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and BorrowingKota Mizushima
 
Scala Macros makes it easy to provide useful libraries
Scala Macros makes it easy to provide useful librariesScala Macros makes it easy to provide useful libraries
Scala Macros makes it easy to provide useful librariesKota Mizushima
 
Scala + Finagleの魅力
Scala + Finagleの魅力Scala + Finagleの魅力
Scala + Finagleの魅力Kota Mizushima
 
Scalaの現状と課題
Scalaの現状と課題Scalaの現状と課題
Scalaの現状と課題Kota Mizushima
 
Scalaでのプログラム開発
Scalaでのプログラム開発Scalaでのプログラム開発
Scalaでのプログラム開発Kota Mizushima
 
日本Scalaユーザーズグループ発足
日本Scalaユーザーズグループ発足日本Scalaユーザーズグループ発足
日本Scalaユーザーズグループ発足Kota Mizushima
 
Implicit Implicit Scala
Implicit Implicit ScalaImplicit Implicit Scala
Implicit Implicit ScalaKota Mizushima
 
Implicit Explicit Scala
Implicit Explicit ScalaImplicit Explicit Scala
Implicit Explicit ScalaKota Mizushima
 
言語アップデート -Scala編-
言語アップデート -Scala編-言語アップデート -Scala編-
言語アップデート -Scala編-Kota Mizushima
 

More from Kota Mizushima (20)

ドワンゴにおける新卒エンジニア向けScala研修について
ドワンゴにおける新卒エンジニア向けScala研修についてドワンゴにおける新卒エンジニア向けScala研修について
ドワンゴにおける新卒エンジニア向けScala研修について
 
kollectionの紹介
kollectionの紹介kollectionの紹介
kollectionの紹介
 
株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状
 
Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -
 
Introduction to PEG
Introduction to PEGIntroduction to PEG
Introduction to PEG
 
Scalaの現状と今後
Scalaの現状と今後Scalaの現状と今後
Scalaの現状と今後
 
Power of Scala
Power of ScalaPower of Scala
Power of Scala
 
Scala Performance Tuning Tips
Scala Performance Tuning TipsScala Performance Tuning Tips
Scala Performance Tuning Tips
 
こわくない型クラス
こわくない型クラスこわくない型クラス
こわくない型クラス
 
こわくないScala
こわくないScalaこわくないScala
こわくないScala
 
Scala is-unscared
Scala is-unscaredScala is-unscared
Scala is-unscared
 
About Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and BorrowingAbout Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and Borrowing
 
Scala Macros makes it easy to provide useful libraries
Scala Macros makes it easy to provide useful librariesScala Macros makes it easy to provide useful libraries
Scala Macros makes it easy to provide useful libraries
 
Scala + Finagleの魅力
Scala + Finagleの魅力Scala + Finagleの魅力
Scala + Finagleの魅力
 
Scalaの現状と課題
Scalaの現状と課題Scalaの現状と課題
Scalaの現状と課題
 
Scalaでのプログラム開発
Scalaでのプログラム開発Scalaでのプログラム開発
Scalaでのプログラム開発
 
日本Scalaユーザーズグループ発足
日本Scalaユーザーズグループ発足日本Scalaユーザーズグループ発足
日本Scalaユーザーズグループ発足
 
Implicit Implicit Scala
Implicit Implicit ScalaImplicit Implicit Scala
Implicit Implicit Scala
 
Implicit Explicit Scala
Implicit Explicit ScalaImplicit Explicit Scala
Implicit Explicit Scala
 
言語アップデート -Scala編-
言語アップデート -Scala編-言語アップデート -Scala編-
言語アップデート -Scala編-
 

Recently uploaded

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

Macros in nemerle

  • 1. Macros in Nemerle Parser Enthusiast Kota Mizushima(@kmizu)
  • 2. 2 Self Introduction Twitter ID: @kmizu GitHub: https://github.com/kmizu Like: PEG, Nemerle, Scala, etc. Interested In: Formal Languages, Parsing
  • 3. 3 What's Nemerle?(1) Object & functional programming language ➢On .NET Framework (CLR or Mono) From: 2003 ➢Old Original Developer: Kamil Skalski, etc. Last stable: v1.2.503.0 ( 2016-03-15 )
  • 4. 4 What's Nemerle?(2) Macro Powerful type inference ➢Can infer type from call-site Pattern matching & variant First-class function , etc.
  • 5. 5 Powerful Type Inferences There exist type inferences in recent languages. Nemerle's type inference is different from: def fact(n){ | 0 => 1 | _ => n * fact(n - 1) } // recursive function ( without type annotation ) fact(4) // int -> int is inferred from use-site
  • 6. 6 What's Macro? Features so-called cacros ➢Lisp's macro ➢Lisp's reader macro ➢Preprocessor macro (in C family language) ➢Hidemaru Macro ← Different meaning ➢Excel Macro(VBA) ← Different meaning Nemerle's macro is inspired by Lisp's macro
  • 7. 7 What's Nemerle's Macros? Lexical level macros ➢[Token] -> [Token] AST level macros ➢ASTNode -> ASTNode Custom attribute macros ➢Global rewriting of program is allowed using such macros ➢Compile time MOP
  • 8. 8 AST Level Macros Similar with Lisp's macros Receive AST of expression and return AST ➢Transformation is written in Nemerle itself ➢Typed AST is handled Many language constructs is just macros ➢If, while, for, using, printf, lock, etc.
  • 9. 9 Hello, World! In Nemerle Statically checked printf using Nemerle.IO; printf("%d + %d = %dn", 1, 2, 1 + 2) //OK printf("%d + %d = %dn", 1, 2, "3") //NG printf("%d + %d = %dn", 1, 2, 3.0) //NG def format = "%d + %d = %dn" printf(format, 1, 2, 3) //NG. format must be string literal
  • 10. 10 Using Macros(1) using System; macro fact(n: int) { def loop(m) {//type inference for recursive function | 0 => 1 | _ => m * f(m - 1) } { def r = f(n); Console.WriteLine("fact({0}" = {1}", n, r); <[ $(r: int) ]> } } fact(4) // → 24
  • 11. 11 Using Macros(2) using System.Text.RegularExpressions; using Nemerle.Compiler macro regex(n: String) { def r = <[ Regex($(n: string)) ]>; try { _ = Regex(n); r; } catch { | _ is ArgumentException => Message.Warning("illegal pattern " + n); r } } regex(".*") // OK regex("**") // → "illegal pattern **" in compile time
  • 12. 12 Using Macros(3) Swap macros macro swap(n, m) syntax("swap", n, "and", m) {// new syntax: swap n and m <[ def tmp = $n; $n = $m; $m = tmp; ]> } using System; mutable x = 10; mutable y = 20; swap x and y; // diffent syntax Console.WriteLine("({0}, {1})", x, y); // → "(20, 10)"
  • 13. 13 Solving Maze in Compile Time(1) using System; using System.Console; using System.IO.File; using Nemerle.Collections; using Nemerle.Imperative; using Nemerle.Compiler; macro SolveMaze(inputFile: String) { def maze = ReadAllLines(inputFile) def h = maze.length; ... }
  • 14. 14 Solving Maze in Compile Time(2) ************************** *S* * * *** * * ************* * * * * ************ * * * * ************** *********** * * ** *********************** * * G * * * *********** * * * * ******* * * * * * ************************** ************************** *S* * $$$$ * *$* *$$* $************* * *$* $$* $$************ * *$$$$* $$$$$ * **************$*********** * $$$$$$$$$$$$$ * **$*********************** * $$$$$* $$$$$$$$$$$$$G * * * $$$$*********** * * * * ******* * * * * * **************************
  • 15. 15 Custome Attribute Macro(Compile Time MOP) using Nemerle.Compiler; using Nemerle.Collections; namespace DesignPatterns { [Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers, Nemerle.MacroTargets.Field)] macro Proxy (t : TypeBuilder, f : FieldBuilder, iface) { ... } } using DesignPatterns; class Point { public X : int { ... }; public Y() : int { ... }; } class PointProxy { [Proxy()] //invocation of Proxy macro point_ : Point; }
  • 16. 16 Lexical Level Macros macro JSONData (group : Token) syntax ("json", group) { ... } ... json({"languages":[ "Java", "Scala", "Nemerle", "C#", "Haskell", "OCaml" ]}) //custom lexer is used
  • 17. 17 Builtin Custom Attribute Macros ProxyPublicMembers macro ➢also known as first class delegation Lazy macro ➢Lazy evalution Memoize macro Etc.
  • 18. 18 Concolusion Overview of Nemerle's macros ➢Lexical level macros ➢AST level macros ➢Custom attribute macros Nemerle is awesome! Try Nemerle! ➢http://nemerle.org