SlideShare a Scribd company logo
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 programmability
Kazumasa Ikuta
 
Scala user-group-19.03.2014
Scala user-group-19.03.2014Scala user-group-19.03.2014
Scala user-group-19.03.2014
Jan Herich
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
vaani pathak
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
Standa Opichal
 
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
Lun-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 tutorial
Freddy 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 talk
John Stevenson
 
Secure code 3rd_party_libs
Secure code 3rd_party_libsSecure code 3rd_party_libs
Secure code 3rd_party_libs
FKM Naimul Huda, PMP
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
Nishant Munjal
 
Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)
Sander Mak (@Sander_Mak)
 
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
Lun-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 language
kiran Patel
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
Paweł Rusin
 
.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
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!
Sander Mak (@Sander_Mak)
 
เมธอด ชั้น ม 6 ห้อง 2
เมธอด ชั้น ม  6 ห้อง 2เมธอด ชั้น ม  6 ห้อง 2
เมธอด ชั้น ม 6 ห้อง 2
Pookie Pook
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of Go
Frank Müller
 
Stack and heap
Stack and heapStack and heap

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

Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
Pierre de Lacaze
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
Diacode
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
John 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 Stevenson
JAX London
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
KamranAli649587
 
Java generics final
Java generics finalJava generics final
Java generics final
Akshay Chaudhari
 
Directive-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous ComputingDirective-based approach to Heterogeneous Computing
Directive-based approach to Heterogeneous Computing
Ruymán Reyes
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
e-Legion
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
Vasil Remeniuk
 
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
Felipe
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
Devnology
 
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
 
Clojure intro
Clojure introClojure intro
Clojure intro
Basav Nagur
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
Prof. Wim Van Criekinge
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
kim.mens
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
Ted Leung
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
Kevlin Henney
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
Jussi Pohjolainen
 

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»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
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
 
kollectionの紹介
kollectionの紹介kollectionの紹介
kollectionの紹介
Kota Mizushima
 
株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状株式会社ドワンゴにおけるScala教育の現状
株式会社ドワンゴにおけるScala教育の現状
Kota Mizushima
 
Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -Scala Daysに行ってみて - あるいはスイス旅行記 -
Scala Daysに行ってみて - あるいはスイス旅行記 -
Kota Mizushima
 
Introduction to PEG
Introduction to PEGIntroduction to PEG
Introduction to PEG
Kota Mizushima
 
Scalaの現状と今後
Scalaの現状と今後Scalaの現状と今後
Scalaの現状と今後
Kota Mizushima
 
Power of Scala
Power of ScalaPower of Scala
Power of Scala
Kota Mizushima
 
Scala Performance Tuning Tips
Scala Performance Tuning TipsScala Performance Tuning Tips
Scala Performance Tuning Tips
Kota Mizushima
 
こわくない型クラス
こわくない型クラスこわくない型クラス
こわくない型クラスKota Mizushima
 
こわくないScala
こわくないScalaこわくないScala
こわくないScala
Kota Mizushima
 
About Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and BorrowingAbout Capabilities for Uniqueness and Borrowing
About Capabilities for Uniqueness and Borrowing
Kota 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 libraries
Kota 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 Scala
Kota 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

“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 

Recently uploaded (20)

“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 

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