Your SlideShare is downloading. ×
0
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Patterns for JVM languages
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Patterns for JVM languages

196

Published on

Have you ever wondered what does it take to create new language? Have you ever wanted to contribute to your favourite one? Don't have time to read "the dragon book"? No problem. …

Have you ever wondered what does it take to create new language? Have you ever wanted to contribute to your favourite one? Don't have time to read "the dragon book"? No problem.

During this talk I will gently introduce you to world of token streams, lexers, parsers and compilers. I will try to show you patterns and tools you have at your disposal, at the same time not diving to deep into theory, rather focusing on practical aspects. Of course I will focus on JVM, bytecode and latest changes in a world of JVM, which make life of language developers easier.

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
196
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. The following presentation is provided under DHMB license (Do Not Hurt My Brain). Lecturer is not responsible for the financial and moral damages resulting from taking too seriously the content of the presentation. Including permanent damage to neurons, reduction of neurotransmitter activity at the molecular level and group dismissal.
  • 2. Patterns for JVM languages Polish JUG, 2014
  • 3. about me work://chief_architect@lumesse owner://symentis.pl twitter://j_palka blog://geekyprimitives.wordpress.com scm:bitbucket://kcrimson scm:github://kcrimson
  • 4. JVM is the new assembler
  • 5. translator interpreter compiler
  • 6. source code ↓ target language source code ↓ machine code || interpreter
  • 7. source code ↓ execution results
  • 8. source code ↓ machine code
  • 9. So how does it all work?
  • 10. source code ↓lexer token stream ↓parser IR
  • 11. Intermediate Representation visitor listener tree
  • 12. Syntax tree
  • 13. local y = 1 x = 1 + y
  • 14. fragment DIGIT : [0-9] ; fragment LETTER : [a-zA-Z] ; String : ''' .*? ''' | '"' .*? '"' ; Number : DIGIT+ ; Name : ( '_' | LETTER ) ( '_' | LETTER | DIGIT )* ; COMMENT : '--' .*? 'n' -> skip ; NL : 'r'? 'n' -> skip ; WS : [ tf]+ -> skip ;
  • 15. [@0,[0..1)='x',<49>] [@1,[2..3)='=',<36>] [@2,[4..5)='1',<48>] [@3,[6..7)='+',<33>] [@4,[8..15)='"Hello"',<47>] [@5,[15..16)=';',<37>] [@6,[17..17)='',<-1=EOF>]
  • 16. varlist returns [Varlist result] : var {$result = createVarlist($var.result);} ( ',' var {$result = createVarlist($result,$var.result);} )* ; // var returns [Var result] : Name { $result = createVariableName($Name.text); } | prefixexp '[' exp ']' | prefixexp '.' Name ;
  • 17. beware of left recursion
  • 18. translator interpreter compiler
  • 19. Interpreter pattern Non terminal nodes Terminal nodes
  • 20. package pl.symentis.lua.grammar; import pl.symentis.lua.runtime.Context; public interface Statement { void execute(Context context); }
  • 21. package pl.symentis.lua.grammar; import pl.symentis.lua.runtime.Context; public interface Expression<T> { T evaluate(Context ctx); }
  • 22. package pl.symentis.lua.runtime; public interface Context { VariableRef resolveVariable(String name); Scope enterScope(); Scope exitScope(); }
  • 23. package pl.symentis.lua.runtime; public interface Scope { Scope getOuterScope(); VariableRef resolveVariable(String name); void bindVariable(String name, Object value); }
  • 24. translator interpreter compiler
  • 25. compiler → byte code org.ow2.asm:asm:jar me.qmx.jitescript:jitescript:jar
  • 26. JiteClass jiteClass = new JiteClass(classname,new String[] { p(Statement.class) }); jiteClass.defineDefaultConstructor(); // parse file and start to walk through AST visitor final CodeBlock codeBlock = newCodeBlock(); chunk.accept(new CompilerVisitor(codeBlock)); codeBlock.voidreturn(); jiteClass.defineMethod("execute", JiteClass.ACC_PUBLIC, sig(void.class, new Class[] { Context.class }), codeBlock); return jiteClass.toBytes(JDKVersion.V1_7);
  • 27. When writing bytecode compiler it helps to think about Java code But at the end you need to think like stack machine :)
  • 28. @Override public void execute(Context context) { x = valueOf(1).plus(valueOf(2)); }
  • 29. public void visitExpression(CodeBlock codeBlock) { codeBlock.ldc(2); codeBlock.invokestatic(p(Integer.class), "valueOf",sig(Integer.class, int.class)); codeBlock.invokestatic(p(LuaTypes.class), "valueOf",sig(LuaType.class,Object.class)); codeBlock.astore(2); codeBlock.ldc(1); codeBlock.invokestatic(p(Integer.class), "valueOf",sig(Integer.class, int.class)); codeBlock.invokestatic(p(LuaTypes.class), "valueOf", sig(LuaType.class, Object.class)); codeBlock.aload(2); codeBlock.invokeinterface(p(LuaType.class), "plus",sig(LuaType.class, LuaType.class)); codeBlock.astore(3); }
  • 30. Symbol table maps variable locals to its symbolic name
  • 31. Pillars of every language design Visibility Types Runtime
  • 32. What's next? Just let me know type analisys and optimizations invokedynamic graal + truffle
  • 33. Or interpreter/compiler hackathon

×