-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlanguages.py
More file actions
3576 lines (3136 loc) · 114 KB
/
languages.py
File metadata and controls
3576 lines (3136 loc) · 114 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8
"""Abstract and concrete classes used to define the different languages."""
import os
import stat
import re
import myshutil as shutil
from helper import print_error, print_info, subprocess_call_wrapper
from codestyles import CODESTYLES
from modeline import get_modelines
from comments import comment_string
from textwrap import dedent
class Language(object):
"""A generic class for programming languages.
Attributes:
name Name of the language (alphanumerical lowercase string)
extensions Extensions (list of string without leading dot). First
extension will be used by default for file creation
information Additional information about the language (useful links)
inline_comment String how inline comments begin
block_comment Pair of string describing how block comments begin and
end
styles Code styles for the language (first one is the default)."""
name = None
extensions = None
information = None
inline_comment = None
block_comment = None
styles = []
@classmethod
def function_not_implemented(cls, function):
"""Default behavior for non-implemented functions."""
print_error('%s not implemented for %s' % (function, cls.name))
return False
@classmethod
def perform_actions(cls, args):
"""Perform the actions on the file - entry point for the class"""
assert args.failure in ['stop', 'continue']
stop_on_failure = args.failure == 'stop'
results = []
for action in args.action:
ret = cls.perform_action(action, args)
results.append((action, ret))
if not ret and stop_on_failure:
break
return results
@classmethod
def perform_action(cls, action, args):
"""Perform a single on the file."""
if hasattr(cls, action):
return getattr(cls, action)(args)
else:
return cls.function_not_implemented(action)
@classmethod
def info(cls, _):
"""Gets information about the language. Wrapper around the information
class member performing some pretty printing."""
if cls.information is None:
return cls.function_not_implemented('info')
assert cls.name is not None
corner, side, top = '+', '|', '-'
title = ' Information about ' + cls.name + ' '
line = corner + top * len(title) + corner
print('\n'.join([line, side + title + side, line, cls.information]))
return True
@classmethod
def get_actual_filename_to_use(cls, args):
"""Returns the filename to be used based on the name and an eventual
extension."""
filename, extmode = args.filename, args.extension_mode
assert extmode in ['auto', 'never', 'always']
# if extension mode is auto, we check if the extension is required
# (if it is not in the list of extensions) and fallback to always/never
if extmode == 'auto':
extmode = 'never' if (
os.path.splitext(filename)[1].lower() in
("." + e.lower() for e in cls.extensions)
) else 'always'
assert extmode in ['never', 'always']
# if extension is required, add the first one
if extmode == 'always':
filename += "." + cls.extensions[0]
return filename
@classmethod
def create(cls, args):
"""Creates and ensures readiness of a file (shebang, boiler-plate code,
execution rights, etc). Wrapper around real_create."""
filename = cls.get_actual_filename_to_use(args)
if os.path.isfile(filename):
assert(args.override_file in ['n', 'y'])
if args.override_file == 'n':
print_info("File %s already exists" % filename)
return True
try:
cls.real_create(filename, args)
if args.filename != filename:
print_info("File %s created" % filename)
return True
except IOError:
print_error("Error while creating file" % filename)
return False
@classmethod
def display(cls, args):
"""Show code that would be displayed when creating the file."""
print(cls.get_content_to_write(args))
return True
@classmethod
def get_code_style(cls, args):
"""Retrieve the relevant code style for the language taking into
account the preference given in the arguments."""
style_arg = args.style
if style_arg is not None:
assert style_arg in CODESTYLES
return CODESTYLES[style_arg]
return cls.styles[0] if cls.styles else {}
@classmethod
def get_content_to_write(cls, args):
"""Get content to be writen in the file - includes header and code."""
mod_pos, editors = args.modeline, args.text_editors
assert mod_pos in ['none', 'top', 'bottom', 'both']
top, bottom = mod_pos in ['both', 'top'], mod_pos in ['both', 'bottom']
modeline = get_modelines(editors, cls.get_code_style(args)) if top or bottom else ''
filename = cls.get_actual_filename_to_use(args)
return cls.get_header_info(modeline if top else '') + \
cls.get_file_content(filename) + \
cls.get_footer_info(modeline if bottom else '')
@classmethod
def real_create(cls, filename, args):
"""Creates and ensures readiness of a file (shebang, boiler-plate code,
execution rights, etc)."""
with open(filename, 'w') as filed:
filed.write(cls.get_content_to_write(args))
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return ""
@classmethod
def get_shebang_line(cls):
"""Return the shebang line.
This method returns an empty string in the default case but can be
overriden (in InterpretableLanguage for instance) to have an actual
shebang on the very first line."""
return ""
@classmethod
def get_header_info(cls, string):
"""Get information to put at the top of the file."""
return cls.get_shebang_line() + \
cls.comment_string(string) + \
cls.comment_string("Generated by letscode")
@classmethod
def get_footer_info(cls, string):
"""Get information to put at the top of the file."""
return cls.comment_string(string)
@classmethod
def comment_string(cls, string):
"""Comment string."""
return comment_string(string, cls.inline_comment, cls.block_comment)
class CompilableLanguage(Language):
"""A generic class for compilable languages.
Attributes:
compiler Command to use to compile
compiler_options Options to give to the compiler."""
compiler = None
compiler_options = []
@classmethod
def man(cls, _):
"""Gets the manual"""
return subprocess_call_wrapper(['man', cls.compiler])
@classmethod
def get_output_filename(cls, filename):
"""Gets the name of the output file"""
return os.path.splitext(filename)[0] + '_out'
@classmethod
def compile(cls, args):
"""Compiles the file"""
filename = cls.get_actual_filename_to_use(args)
output = cls.get_output_filename(filename)
return subprocess_call_wrapper(
[cls.compiler] + cls.compiler_options + [filename, '-o', output])
@classmethod
def run(cls, args):
"""Runs the code"""
# We do not look in '.' by default, let's use the abs path
output = os.path.abspath(
cls.get_output_filename(
cls.get_actual_filename_to_use(args)))
return subprocess_call_wrapper([output])
@classmethod
def is_ready(cls):
"""Check if language is 'ready' (as in compiler can be found)."""
return shutil.which(cls.compiler) is not None
class CompiledDescriptionLanguages(CompilableLanguage):
"""A generic class for compiled descriptions languages : a compiler is used
but there is nothing to run, just files to open."""
@classmethod
def run(cls, args):
"""Checks that the output file exists"""
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
if os.path.isfile(output):
print_info("File %s can be open" % output)
return True
else:
print_error("File %s does not exist" % output)
return False
class CLanguage(CompilableLanguage):
"""C"""
name = 'c'
code_extensions = ['c']
header_extensions = ['h']
extensions = code_extensions + header_extensions
compiler = 'gcc'
compiler_options = ['-Wall', '-Wextra', '-std=c99']
inline_comment = '//'
block_comment = ('/*', '*/')
styles = [CODESTYLES['c-kernel'], CODESTYLES['c-apache']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/C_%28programming_language%29
- Official site :
- Documentation :
* FAQ : http://www.c-faq.com/
- Subreddit : http://www.reddit.com/r/C_Programming/
- Tools online :
* Compiler (with ASM output - no run) : http://gcc.godbolt.org/
* Compiler (with ASM output - no run) : http://assembly.ynh.io/
* C gibberish <-> English : http://cdecl.org/
* Demangler : http://demangler.com/
* Online compiler (run) : http://coliru.stacked-crooked.com/
- Learn in Y minutes : http://learnxinyminutes.com/docs/c/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:C
* Progopedia : http://progopedia.com/language/c/
* RosettaCode : http://rosettacode.org/wiki/Category:C
''')
@classmethod
def debug(cls, args):
"""Launches the debugger"""
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
return subprocess_call_wrapper(['gdb', output])
@classmethod
def pretty(cls, args):
"""Makes the code prettier"""
filename = cls.get_actual_filename_to_use(args)
# or maybe using astyle
return subprocess_call_wrapper(['indent', filename])
@classmethod
def get_header_content(cls, symbol):
"""Gets code for a header file"""
return dedent('''
#ifndef %s
#define %s
#endif
''') % (symbol, symbol)
@classmethod
def get_code_content(cls, included):
"""Gets code for an implementation file"""
return dedent('''
#include <stdio.h>
//#include "%s"
int main(int argc, char* argv[])
{
printf("Hello, world!\\n");
return 0;
}
''') % included
@classmethod
def get_file_content(cls, filename):
"""Returns the content to be put in the file."""
# Different file content for implementations files and headers
realfilename = os.path.split(filename)[1]
base, ext = os.path.splitext(realfilename)
if ext.lower() in ('.' + e for e in cls.header_extensions):
return cls.get_header_content(
'__%s__' % re.sub('[^A-Z]', '_', base.upper()))
else:
assert ext.lower() in ('.' + e for e in cls.code_extensions)
return cls.get_code_content(base + '.' + cls.header_extensions[0])
@classmethod
def check(cls, args):
"""Calls static checker"""
filename = cls.get_actual_filename_to_use(args)
commands = {
'cppcheck': ['--enable=all']
}
return_values = [
subprocess_call_wrapper([c] + opt + [filename])
for c, opt in commands.items()]
return all(return_values)
@classmethod
def metrics(cls, args):
"""Gets metrics for code"""
# to be done cppncss and cccc
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(['c_count', filename])
class ObjectiveC(CLanguage):
"""ObjectiveC"""
name = 'objectivec'
code_extensions = ['m', 'mm']
header_extensions = ['h']
extensions = code_extensions + header_extensions
compiler_options = ['-Wall', '-Wextra']
styles = [CODESTYLES['objc-google']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Objective-C
- Official site : https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
- Documentation :
- Tools online :
* Try Objective C: http://tryobjectivec.codeschool.com/
* Compile online : http://www.compileonline.com/compile_objective-c_online.php
- Subreddit : http://www.reddit.com/r/ObjectiveC/
- Learn in Y minutes : http://learnxinyminutes.com/docs/objective-c/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Objective-C
* Progopedia : http://progopedia.com/language/objective-c/
* RosettaCode : http://rosettacode.org/wiki/Category:Objective-C
''')
class Cpp(CLanguage):
"""Cpp"""
name = 'cpp'
code_extensions = ['cpp', 'cc', 'cxx', 'c++']
header_extensions = ['hpp', 'hh', 'h', 'hxx', 'h++']
extensions = code_extensions + header_extensions
compiler = 'g++'
compiler_options = ['-Wall', '-Wextra']
styles = [CODESTYLES['cpp-google'], CODESTYLES['cpp-boost']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/C++
- Official site : http://isocpp.org/
- Documentation : http://en.cppreference.com/
- Misc :
* Guru of the week : http://www.gotw.ca/gotw/
- Subreddit : http://www.reddit.com/r/cpp/
- Learn in Y minutes : http://learnxinyminutes.com/docs/c++/
- Tools online :
* Compiler (with ASM output - no run) : http://gcc.godbolt.org/
* Compiler (with ASM output - no run) : http://assembly.ynh.io/
* C gibberish <-> English : http://cdecl.org/
* Demangler : http://demangler.com/
* Online compiler (run) : http://coliru.stacked-crooked.com/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:C_Plus_Plus
* Progopedia : http://progopedia.com/language/c-plus-plus/
* RosettaCode : http://rosettacode.org/wiki/Category:C%2B%2B
''')
@classmethod
def get_code_content(cls, included):
"""Gets code for an implementation file"""
return dedent('''
#include <iostream>
//#include "%s"
int main(int argc, char* argv[])
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
''') % included
class Java(CompilableLanguage):
"""Java"""
name = 'java'
extensions = ['java', 'class', 'jar']
compiler = 'javac' # support for gcj could be added if needed
inline_comment = '//'
block_comment = ('/*', '*/')
styles = [CODESTYLES['java-google'], CODESTYLES['java-oracle']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Java_%28programming_language%29
- Official site : http://www.java.com/
- Documentation : http://docs.oracle.com/javase/7/docs/api/
- Subreddit : http://www.reddit.com/r/java/
- Tools online :
* Paste and run : https://www.ktbyte.com/paste
* Visualiser : http://cscircles.cemc.uwaterloo.ca/java_visualize/
* Visualiser : http://visualize.learneroo.com/
* Demangler : http://demangler.com/
* Javabytes (disassembler) : http://javabytes.herokuapp.com/
* REPL : http://www.javarepl.com/console.html
- Learn in Y minutes : http://learnxinyminutes.com/docs/java/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Java
* Progopedia : http://progopedia.com/language/java/
* RosettaCode : http://rosettacode.org/wiki/Category:Java
- Misc ressources :
* Hidden features (StackOverflow) : http://stackoverflow.com/questions/15496/hidden-features-of-java
''')
@classmethod
def get_output_filename(cls, filename):
"""Gets the name of the output file"""
return cls.get_classfile(filename) + '.class'
@classmethod
def get_classfile(cls, filename):
"""Gets the name of the file without extensions (name of the class)"""
return os.path.splitext(filename)[0]
@classmethod
def get_file_content(cls, filename):
"""Returns the content to be put in the file."""
classname = os.path.split(cls.get_classfile(filename))[1]
return dedent('''
public class %s {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}''') % classname
@classmethod
def compile(cls, args):
"""Compiles the file"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(
[cls.compiler, filename] + cls.compiler_options)
@classmethod
def check(cls, args):
"""Calls static checker"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(
[cls.compiler, filename] + cls.compiler_options + ['-Xlint:all'])
@classmethod
def run(cls, args):
"""Runs the code"""
classfile = cls.get_classfile(cls.get_actual_filename_to_use(args))
classpath, classname = os.path.split(classfile)
return subprocess_call_wrapper(
['java', '-enableassertions', '-classpath', classpath, classname])
@classmethod
def debug(cls, args):
"""Launches the debugger"""
classfile = cls.get_classfile(cls.get_actual_filename_to_use(args))
classpath, classname = os.path.split(classfile)
return subprocess_call_wrapper(
['jdb', '-classpath', classpath, classname])
class Vala(CompilableLanguage):
"""Vala"""
name = 'vala'
extensions = ['vala', 'vapi']
compiler = 'valac'
inline_comment = '//'
block_comment = ('/*', '*/')
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Vala_%28programming_language%29
- Official site : https://wiki.gnome.org/Projects/Vala
- Documentation : https://wiki.gnome.org/Projects/Vala/Documentation
- Subreddit : http://www.reddit.com/r/vala
- Code samples :
* RosettaCode : http://rosettacode.org/wiki/Category:Vala
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
int main () {
print ("Hello, world!\\n");
return 0;
}
''')
class Pascal(CompilableLanguage):
"""Pascal"""
name = 'pascal'
extensions = ['pas']
compiler = 'fpc'
inline_comment = '//'
block_comment = ('(*', '*)') # or ('{', '}')
styles = [CODESTYLES['pascal-gnu']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Pascal_%28programming_language%29
- Official site :
- Documentation :
- Subreddit :
* http://www.reddit.com/r/pascal/
* http://www.reddit.com/r/delphi/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Pascal
* Progopedia : http://progopedia.com/language/pascal/
* RosettaCode : http://rosettacode.org/wiki/Category:Pascal
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
program HelloWorld;
begin
writeln('Hello, world!');
end.
''')
@classmethod
def compile(cls, args):
"""Compiles the file"""
filename = cls.get_actual_filename_to_use(args)
output = cls.get_output_filename(filename)
return subprocess_call_wrapper(
[cls.compiler] + cls.compiler_options + [filename, '-o' + output])
@classmethod
def debug(cls, args):
"""Launches the debugger"""
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
return subprocess_call_wrapper(['gdb', output])
class Ada(CompilableLanguage):
"""Ada"""
name = 'ada'
extensions = ['adb', 'ads']
compiler = 'gnat' # many options - documentation with 'html' for instance
# also for https://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Style-Checking.html
compiler_options = ['make']
inline_comment = '--'
styles = [CODESTYLES['ada-gnat']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Ada_(programming_language)
- Official site : http://www.adaic.org/
- Documentation :
- Subreddit : http://www.reddit.com/r/ada/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Ada
* Progopedia : http://progopedia.com/language/ada/
* RosettaCode : http://rosettacode.org/wiki/Category:Ada
''')
@classmethod
def get_procname(cls, filename):
"""Gets the name of the file without extensions (name of the procedure)"""
return os.path.splitext(filename)[0]
@classmethod
def get_file_content(cls, filename):
"""Returns the content to be put in the file."""
procname = os.path.split(cls.get_procname(filename))[1]
return dedent('''
with Ada.Text_IO; use Ada.Text_IO;
procedure %s is
begin
Put_Line ("Hello, world!");
end %s;
''') % (procname, procname)
class Fortran(CompilableLanguage):
"""Fortran"""
name = 'fortran'
extensions = ['f', 'for', 'f90', 'f95']
compiler = 'gfortran'
compiler_options = ['--free-form']
inline_comment = '!'
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Fortran
- Official site :
- Documentation : http://www.fortran90.org/
- Subreddit : http://www.reddit.com/r/fortran/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Fortran
* Progopedia : http://progopedia.com/language/fortran/
* RosettaCode : http://rosettacode.org/wiki/Category:Fortran
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
program helloworld
print *, "Hello, world!"
end program helloworld
''')
@classmethod
def debug(cls, args):
"""Launches the debugger"""
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
return subprocess_call_wrapper(['gdb', output])
@classmethod
def metrics(cls, args):
"""Gets metrics for code"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(['fortran_count', filename])
class Cobol(CompilableLanguage):
"""Cobol"""
name = 'cobol'
extensions = ['cob', 'cbl']
inline_comment = ' *'
compiler = 'cobc'
compiler_options = ['-x']
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/COBOL
- Subreddit : http://www.reddit.com/r/cobol
- Tools online :
* Compile online : http://www.compileonline.com/compile_cobol_online.php
- Code samples :
* Progopedia : http://progopedia.com/language/cobol/
* RosettaCode : http://rosettacode.org/wiki/Category:COBOL
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
# No dedent here - leading spaces matter
return '''
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY 'Hello, world!'.
STOP RUN.
'''
@classmethod
def metrics(cls, args):
"""Gets metrics for code"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(['cobol_count', filename])
# hlint
class Haskell(CompilableLanguage):
"""Haskell"""
name = 'haskell'
extensions = ['hs', 'lhs']
compiler = 'ghc'
inline_comment = '--'
block_comment = ('{-', '-}')
styles = [CODESTYLES['haskell']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Haskell_%28programming_language%29
- Official site : http://www.haskell.org/
- Documentation : http://www.haskell.org/ghc/docs/7.6-latest/html/libraries/index.html
- Subreddit : http://www.reddit.com/r/haskell/
- Tools online :
* Try Haskell : http://tryhaskell.org/
- Learn in Y minutes : http://learnxinyminutes.com/docs/haskell/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:Haskell
* Progopedia : http://progopedia.com/language/haskell/
* RosettaCode : http://rosettacode.org/wiki/Category:Haskell
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
main = putStrLn "Hello, world!"
''')
@classmethod
def debug(cls, args):
"""Launches the debugger"""
# Another option is to use GHCi
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
return subprocess_call_wrapper(['gdb', output])
@classmethod
def metrics(cls, args):
"""Gets metrics for code"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(['haskell_count', filename])
class DLanguage(CompilableLanguage):
"""D"""
name = 'd'
extensions = ['d']
compiler = 'gdc'
inline_comment = '//'
block_comment = ('/*', '*/')
styles = [CODESTYLES['d']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/D_%28programming_language%29
- Official site : http://dlang.org/
- Documentation :
* Language reference : http://dlang.org/spec.html
* Library reference : dlang.org/phobos/index.html
- Subreddit : http://www.reddit.com/r/d_language/
- Tools online :
* D Paste : http://dpaste.dzfl.pl/
* Compile Online : http://www.compileonline.com/compile_d_online.php
* ASM : http://asm.dlang.org/
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:D
* Progopedia : http://progopedia.com/language/d/
* RosettaCode : http://rosettacode.org/wiki/Category:D
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
import std.stdio;
void main()
{
writeln("Hello, world!");
}
''')
@classmethod
def debug(cls, args):
"""Launches the debugger"""
output = cls.get_output_filename(cls.get_actual_filename_to_use(args))
return subprocess_call_wrapper(['gdb', output])
class AppleScript(Language):
"""AppleScript"""
name = 'applescript'
extensions = ['scpt', 'AppleScript']
inline_comment = '--' # and more recently '#'
block_comment = ('(*', '*)')
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/AppleScript
- Official site : http://www.macosxautomation.com/applescript/
- Documentation : https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html
- Subreddit : http://www.reddit.com/r/applescript
- Code samples :
* LiteratePrograms : http://en.literateprograms.org/Category:Programming_language:AppleScript
* RosettaCode : http://rosettacode.org/wiki/Category:AppleScript
''')
class Qi(Language):
"""Qi"""
name = 'qi'
extensions = ['qi']
block_comment = ('\\', '\\')
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Qi_(programming_language)
- Official site : http://www.lambdassociates.org/
- Code samples :
* RosettaCode : http://rosettacode.org/wiki/Category:Qi
''')
class Shen(Language):
"""Shen"""
name = 'shen'
extensions = ['shen']
inline_comment = '\\\\'
block_comment = ('\\*', '*\\')
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Shen_(programming_language)
- Official site : http://www.shenlanguage.org/
- Documentation : http://www.shenlanguage.org/learn-shen/index.html
- Code samples :
* RosettaCode : http://rosettacode.org/wiki/Category:Shen
''')
class MarkupLanguage(Language):
"""A generic class for markup languages"""
block_comment = ('<!--', '-->')
class HTML(MarkupLanguage):
"""HTML"""
name = 'html'
extensions = ['html']
styles = [CODESTYLES['html-google']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/HTML
- Official site : http://www.w3.org/
- Subreddit : http://www.reddit.com/r/html
- Tools online :
* W3C Validator : http://validator.w3.org/
* HTML Obfuscator : http://htmlobfuscator.com/
* HTML 2 Jade : http://html2jade.org/
''')
@classmethod
def pretty(cls, args):
"""Makes the code prettier"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(
['xmllint', '--format', '--html', filename])
class XML(MarkupLanguage):
"""XML"""
name = 'xml'
extensions = ['xml']
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/XML
- Tools online :
* XML Playground : http://xmlplayground.com/
* XML Beautify/Viewer : http://codebeautify.org/xmlviewer/
- Learn in Y minutes : http://learnxinyminutes.com/docs/xml/
''')
@classmethod
def pretty(cls, args):
"""Makes the code prettier"""
filename = cls.get_actual_filename_to_use(args)
return subprocess_call_wrapper(['xmllint', '--format', filename])
class CSS(Language):
"""CSS"""
name = 'css'
extensions = ['css']
block_comment = ('/*', '*/')
styles = [CODESTYLES['css']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Cascading_Style_Sheets
- Official site : http://www.w3.org/Style/CSS/Overview.en.html
- Documentation :
- Tools online :
* CSS Lint : http://csslint.net/
* CSS Desk : http://www.cssdesk.com/
* CSS Comb (in progress) : http://csscomb.com/
- Learn in Y minutes : http://learnxinyminutes.com/docs/css/
''')
class JSON(Language):
"""JSON"""
name = 'json'
extensions = ['json']
# No comment in JSON
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/JSON
- Official site : http://www.json.org/
- Documentation :
- Tools online :
* JSON Lint : http://jsonlint.com/
* JSON Fiddle : http://jsonfiddle.net/
* JSON Schema Lint : http://jsonschemalint.com
* JSON Selector : http://jsonselector.com/
* Geo JSON Lint : http://geojsonlint.com/
* JSON Processor : https://jqplay.org/
- Learn in Y minutes : http://learnxinyminutes.com/docs/json/
''')
class YAML(Language):
"""YAML"""
name = 'yaml'
extensions = ['yaml', 'yml']
inline_comment = '#'
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/YAML
- Official site : http://yaml.org/
- Documentation : http://www.yaml.org/spec/1.2/spec.html
- Tools online :
* YAML Lint : http://yamllint.com/
* Online parser : http://yaml-online-parser.appspot.com/
* YAML to JSON : http://yamltojson.com/
* JSON to YAML : http://jsontoyaml.com/
- Learn in Y minutes : http://learnxinyminutes.com/docs/yaml/
''')
class HAML(Language):
"""HAML (HTML Abstraction Markup Language)"""
name = 'haml'
extensions = ['haml']
inline_comment = '-#'
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/Haml
- Official site : http://haml.info/
- Documentation : http://haml.info/docs.html
- Tools online :
* HAML to ERB : http://haml2erb.herokuapp.com/
* HAML to HTML : http://www.haml-converter.com/
* HTML to HAML : http://htmltohaml.com/
''')
class reST(CompiledDescriptionLanguages):
"""reStructuredText"""
name = 'rest'
extensions = ['rst']
inline_comment = '..'
compiler = 'rst2html'
# other compilers : rst2html rst2latex rst2man rst2odt rst2odt_prepstyles
# rst2pseudoxml rst2s5 rst2xetex rst2xml
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/ReStructuredText
- Official site : http://docutils.sourceforge.net/rst.html
- Documentation : http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
- Cheatsheet : http://docutils.sourceforge.net/docs/user/rst/cheatsheet.txt
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return dedent('''
Section Header
==============
Subsection Header
-----------------
Hello, world!
''')
@classmethod
def get_output_filename(cls, filename):
"""Gets the name of the output file"""
return os.path.splitext(filename)[0] + '.html'
@classmethod
def compile(cls, args):
"""Compiles the file"""
filename = cls.get_actual_filename_to_use(args)
output = cls.get_output_filename(filename)
return subprocess_call_wrapper(
[cls.compiler] + cls.compiler_options + [filename, output])
class CoffeeScript(Language):
"""CoffeeScript"""
name = 'coffeescript'
extensions = ['coffee']
inline_comment = '#'
shell_stop = 'process.exit()' # not relevant but in order not to forget
styles = [CODESTYLES['coffeescript']]
information = dedent('''
- Wikipedia page : http://en.wikipedia.org/wiki/CoffeeScript
- Official site : http://coffeescript.org/
- Documentation :
- Tools online :
* JS to Coffee : http://js2coffee.org/
* Coffee Lint : http://www.coffeelint.org/
* Try Coffee Script : http://coffeescript.org/
- Learn in Y minutes : http://learnxinyminutes.com/docs/coffeescript/
- Subreddit : http://www.reddit.com/r/coffeescript/
''')
@classmethod
def get_file_content(cls, _):
"""Returns the content to be put in the file."""
return JavaScript.get_file_content(_)
class Move(Language):
"""Move"""
name = 'move'
extensions = ['mv']