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

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 

Recently uploaded (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

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