<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2012 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# provides tools to write C .c and .h files
module c_tools

import compiling_writer

# Accumulates all C code for a compilation unit
class CCompilationUnit
	## header
	# comments and native interface imports
	var header_c_base : Writer = new Writer

	# custom C header code or generated for other languages
	var header_custom : Writer = new Writer

	# types of extern classes and friendly types
	var header_c_types : Writer = new Writer

	# implementation declaration for extern methods
	var header_decl : Writer = new Writer

	## body
	# comments, imports, etc
	var body_decl : Writer = new Writer

	# custom code and generated for ffi
	var body_custom : Writer = new Writer

	# implementation body of extern methods
	var body_impl : Writer = new Writer

	# files to compile TODO check is appropriate
	var files = new Array[String]

	fun add_local_function( efc : CFunction )
	do
		body_decl.add( "{efc.signature};\n" )
		body_impl.add( "\n" )
		body_impl.append( efc.to_writer )
	end

	fun add_exported_function( efc : CFunction )
	do
		header_decl.add( "{efc.signature};\n" )
		body_impl.add( "\n" )
		body_impl.append( efc.to_writer )
	end

	fun compile_header_core( stream : OStream )
	do
		header_c_base.write_to_stream( stream )
		header_custom.write_to_stream( stream )
		header_c_types.write_to_stream( stream )
		header_decl.write_to_stream( stream )
	end

	fun compile_body_core( stream : OStream )
	do
		body_decl.write_to_stream( stream )
		body_custom.write_to_stream( stream )
		body_impl.write_to_stream( stream )
	end
end

# Accumulates C code related to a specific function
class CFunction
	var signature : String

	var decls : Writer = new Writer
	var exprs : Writer = new Writer

	fun to_writer : Writer
	do
		var w = new Writer

		w.add( "{signature}\n\{\n" )

		w.append( decls )
		w.add( "\n" )
		w.append( exprs )

		w.add( "\}\n" )

		return w
	end
end<commit_msg>src/c_tools: use template instead of compiling_writer<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2012 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# provides tools to write C .c and .h files
module c_tools

import template

# Accumulates all C code for a compilation unit
class CCompilationUnit
	## header
	# comments and native interface imports
	var header_c_base = new Template

	# custom C header code or generated for other languages
	var header_custom = new Template

	# types of extern classes and friendly types
	var header_c_types = new Template

	# implementation declaration for extern methods
	var header_decl = new Template

	## body
	# comments, imports, etc
	var body_decl = new Template

	# custom code and generated for ffi
	var body_custom = new Template

	# implementation body of extern methods
	var body_impl = new Template

	# files to compile TODO check is appropriate
	var files = new Array[String]

	fun add_local_function( efc : CFunction )
	do
		body_decl.add( "{efc.signature};\n" )
		body_impl.add( "\n" )
		body_impl.add( efc.to_writer )
	end

	fun add_exported_function( efc : CFunction )
	do
		header_decl.add( "{efc.signature};\n" )
		body_impl.add( "\n" )
		body_impl.add( efc.to_writer )
	end

	fun compile_header_core( stream : OStream )
	do
		header_c_base.write_to( stream )
		header_custom.write_to( stream )
		header_c_types.write_to( stream )
		header_decl.write_to( stream )
	end

	fun compile_body_core( stream : OStream )
	do
		body_decl.write_to( stream )
		body_custom.write_to( stream )
		body_impl.write_to( stream )
	end
end

# Accumulates C code related to a specific function
class CFunction
	var signature : String

	var decls = new Template
	var exprs = new Template

	fun to_writer: Template
	do
		var w = new Template

		w.add(signature)
		w.add("\n\{\n")
		w.add(decls)
		w.add("\n")
		w.add(exprs)
		w.add("\}\n")

		return w
	end
end
{'fraction_non_alphanumeric': 0.08001600320064013, 'fraction_numerical': 0.003200640128025605, 'mean_word_length': 3.854368932038835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7009163', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1548, 'n_words': 585}